Results 1 to 10 of 10

Thread: Firefox hates my Javascript

  1. #1
    Join Date
    Oct 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Firefox hates my Javascript

    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)
    Last edited by CCH; 01-08-2010 at 08:49 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here you go.
    Code:
    <script type="text/javascript">
    window.onload = function(){
      this.location.href = "#partofpage";
    }
    </script>
    Jeremy | jfein.net

  3. #3
    Join Date
    Oct 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Here you go.
    Code:
    <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.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You don't need the window.onload event - just use WINDOW.location.href.
    Jeremy | jfein.net

  5. #5
    Join Date
    Oct 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Please post your code and a link to your page. No further assistance till that link gets posted.
    Jeremy | jfein.net

  7. #7
    Join Date
    Oct 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    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.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    inter = setInterval("document.getElementById('phpbb').fireEvent('onMouseMove')",10);
    This appears to mean to fire this:

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

    Code:
    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):

    Code:
    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:

    Code:
    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).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Oct 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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)

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by CCH View Post
    (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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •