PDA

View Full Version : Resolved Firefox hates my Javascript



CCH
01-08-2010, 02:07 AM
I wrote a code for a page that's supposed to modify the current page's url by adding a link to it (#link). it works perfectly in IE, but when the script runs in Firefox, it refuses to update the url. My syntax should be correct, since if I put a normal url in instead of the # link, it works.

here's the basic idea of my code:

[Document URL before script is run: http://website.com]

{script code}
document.URL='#partofpage';

[IE's URL after script: http://website.com#partofpage]
[Firefox's URL after script: http://website.com]


IE does what I want, but I can't seem to get Firefox to add the link to the url. I've also tried using the full URL in the script, and Firefox just seems to ignore it. No errors, it just doesn't do anything.
(I've also tried "document.location" instead of "document.URL" and the same results)

Nile
01-08-2010, 02:17 AM
Here you go.


<script type="text/javascript">
window.onload = function(){
this.location.href = "#partofpage";
}
</script>

;)

CCH
01-08-2010, 02:22 AM
Here you go.


<script type="text/javascript">
window.onload = function(){
this.location.href = "#partofpage";
}
</script>

;)

I don't need the change to happen with the window.onLoad event. It needs to happen in another function after the page has loaded.
I tried modifying my script with "document.location.href" instead of "document.url", but firefox still doesn't respond.

Nile
01-08-2010, 02:23 AM
You don't need the window.onload event - just use WINDOW.location.href.

CCH
01-08-2010, 02:49 AM
That part worked, however I now have a new problem.
The event that triggers the script is from an iframe. I used the onReadyStateChange event, which works fine in IE, but doesn't seem to exist in Firefox. I don't want to use the onLoad event on the iframe, since the script needs to run before the iframe has finished loading the page.

Is there any way to get firefox to register the onreadystatechange event to an iframe? I know the event exists because it works in AJAX requests.
Am I going to be forced to use the onLoad event, just because Firefox doesn't like the onReadyStateChange event?

So far, I have both events set up. In IE, it doesn't hurt anything and the script runs exactly as intended, however, in Firefox, while the script works, it's delayed until the iframe has finished loading, resulting in what appears to be a odd lag.

Nile
01-08-2010, 03:11 AM
Please post your code and a link to your page. No further assistance till that link gets posted. ;)

CCH
01-08-2010, 03:34 AM
Please post your code and a link to your page. No further assistance till that link gets posted. ;)

http://mycrashpoint.com/testingframe.html

This is the page I'm using to test the script before it goes live on my site. The goal is to create something similar to the bar used on Facebook.

All I really need to know is how to make Firefox recognize the onReadyStateChange event on an iFrame.

jscheuer1
01-08-2010, 07:21 AM
This is the error from Firefox:


Error: document.getElementById("phpbb").fireEvent is not a function
Source File: http://mycrashpoint.com/forums/barupd.js
Line: 47

This means that fireEvent is neither a native function to javascript, which it isn't, not even in IE, nor a function defined previously in your code.

Here's line 47:


inter = setInterval("document.getElementById('phpbb').fireEvent('onMouseMove')",10);

This appears to mean to fire this:


<body onMouseMove="fixit2(event)" onKeyDown="fixit()" onMouseOver="startInt()" onMouseOut="stopInt()" id="phpbb" class="section-Index ltr">


Which is:


function fixit2(event)
{
if (x==event.screenX && y==event.screenY)
{
return;
}
fixit();
x=event.screenX;
y=event.screenY;
}

However, there will never be an event.screen X or Y unless this function is actually fired by the user moving their mouse over the page, or unless you create a pseudo event object (highlighted) for the function. You could do that like so (replacing line 47 with):


inter = setInterval(function(){fixit2({screenX: 0, screenY: 0});}, 10);

Change 0 to whatever value you might want it to be for screenX and for screenY, but 0 could be fine.

All this seems to want to do is to keep the page 'awake' if the user isn't moving their mouse. If that's the case, dispense with everything that could put the page to sleep in the first place instead. Otherwise, just let the page sleep if it wants to (get rid of the line 47 bit and any other code that depends directly upon it).



On another front, the best way to get your URL in the address bar to change itself to something like:


http://website.com#partofpage

which is called a hash BTW, is to change the hash:


location.hash = 'partofpage';

This will not reload the page. It will produce a click sound in IE if IE is so configured (the default in that browser).

CCH
01-08-2010, 08:15 AM
As expected... those parts of the code work exactly as I intended and don't need to be changed. The only thing I was asking for to have changed was getting Firefox to call a function when the iframe starts navigation, not when it finishes (as with the onLoad event). IE's onReadyStateChange event does this perfectly, but Firefox does not seem to have any function that can do what I want.

Either way, I was able to get things to work well enough to deal with it.

(not to sound unappreciative, but next time someone asks for help, don't start picking apart codes that have nothing to do with what was asked)

jscheuer1
01-08-2010, 08:55 AM
(not to sound unappreciative, but next time someone asks for help, don't start picking apart codes that have nothing to do with what was asked)

It does sound that way. Perhaps, if that's the way you feel, I will not bother looking at future requests from you. The error is real. It may be causing problems or could cause problems in the future.

I generally like to get rid of the obvious errors before tackling any other issues, as errors have a funny way of often preventing otherwise good code from executing as expected.

But, as I say, and as I think you are saying, if you don't like that approach, there is no need for me to look at future requests for help from you.