www.webdeveloper.com
Results 1 to 8 of 8

Thread: Conditional execution problem on website

  1. #1
    Join Date
    Oct 2011
    Posts
    2

    Conditional execution problem on website

    I have a wordpress website bloomingtonsouth.org where I am trying to put in some javascript in my header file. What I am trying to do is create a function (called by the Print Edition, Click Here links at the top left of my site) that checks the url of the website, and if it is the homepage, scrolls down, and if it is any other page, directs the browser to a link. Here is my code:
    Code:
    function jumpScroll() {
    
    var url = window.location.href;
    if (url == "http://bloomingtonsouth.org/")
    {
    	alert("Executing If Statement");
    	window.scroll(0,1300); // horizontal and vertical scroll targets;
    * }
    *
    else
     {
    	alert("Executing Else Statement");
    	window.location = "http://www.bloomingtonsouth.org/?page_id=2039"
      }
    
    }
    It seems that for some reason or another, the if function keeps coming up false even though it's true, as you can tell by the fact that the "Executing Else Statement" alert comes up on the homepage. Does anyone see where the flaw in my code is. It's worth noting that the header file is a php and the version of javascript is 1.2.

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Try alerting location.href and see if you can find a difference from the string to which you compare it.

    The way your site is organised, I think a better test would be:

    Code:
    if( location.search == "" )
     ...

  3. #3
    Join Date
    Oct 2011
    Posts
    2
    Quote Originally Posted by Logic Ali View Post
    Try alerting location.href and see if you can find a difference from the string to which you compare it.

    The way your site is organised, I think a better test would be:

    Code:
    if( location.search == "" )
     ...
    I tried the alert like you said earlier weirdly enough they were the same. However I tried you're recommended code and it did the trick. Thanks!

  4. #4
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    with typos like
    Code:
    * }
    *
    I am sure that it does not work.

    I feel that
    Code:
    document.location.replace(url);
    will function better than passing a URL to document.location. In my experience, some browsers ignore this and Opera which I mainly use didn't like that method and I used the .replace in place of that and the page redirect perfectly.

  5. #5
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by JunkMale View Post
    with typos like
    Code:
    * }
    *
    I am sure that it does not work.

    I feel that
    Code:
    document.location.replace(url);
    will function better than passing a URL to document.location. In my experience, some browsers ignore this and Opera which I mainly use didn't like that method and I used the .replace in place of that and the page redirect perfectly.
    The code on the site doesn't have the *.

    Writing to window.location.href should not give problems.

  6. #6
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Quote Originally Posted by Logic Ali View Post
    The code on the site doesn't have the *.

    Writing to window.location.href should not give problems.
    The point I was making was that some browsers won't window.location.href = "someurlref"; as I found, it wasn't until I changed the method that it worked, so its a good idea to keep that in mind.

  7. #7
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by JunkMale View Post
    The point I was making was that some browsers won't window.location.href = "someurlref"; as I found, it wasn't until I changed the method that it worked, so its a good idea to keep that in mind.
    Do you have an example of a current browser that doesn't. ISTR Opera 5.* didn't.

  8. #8
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    When I wrote the example code. Uploaded to the server and page delivered to Opera 11.52 would not redirect at all. So I used .replace() and it then worked.

    The update from 10.x may have bugs, I don't know, all I can report is "My Experience" and thats all I can report. Examples? Where would one get an example of a browser not playing ball from?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles