/    Sign up×
Community /Pin to ProfileBookmark

Conditional execution problem on website

I have a wordpress website [URL=”http://www.bloomingtonsouth.org/”]bloomingtonsouth.org[/URL] 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”
}

}[/CODE]

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.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliOct 28.2011 — Try alerting [FONT=Courier New]location.href[/FONT] 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:

if( location.search == "" )
...
Copy linkTweet thisAlerts:
@ian9912authorOct 28.2011 — Try alerting [FONT=Courier New]location.href[/FONT] 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:

if( location.search == "" )
...
[/QUOTE]


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!
Copy linkTweet thisAlerts:
@JunkMaleOct 28.2011 — with typos like [CODE]* }
*[/CODE]
I am sure that it does not work.

I feel that [CODE]document.location.replace(url);[/CODE] 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.
Copy linkTweet thisAlerts:
@Logic_AliOct 28.2011 — with typos like * }
*
I am sure that it does not work.

I feel that 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.[/quote]


The code on the site doesn't have the *.

Writing to [FONT=Courier New]window.location.href[/FONT] should not give problems.
Copy linkTweet thisAlerts:
@JunkMaleOct 28.2011 — The code on the site doesn't have the *.

Writing to [FONT=Courier New]window.location.href[/FONT] should not give problems.[/QUOTE]


The point I was making was that some browsers won't [FONT="Fixedsys"][I]window.location.href = "someurlref";[/I][/FONT] as I found, it wasn't until I changed the method that it worked, so its a good idea to keep that in mind.
Copy linkTweet thisAlerts:
@Logic_AliOct 28.2011 — The point I was making was that some browsers won't [FONT=Fixedsys][I]window.location.href = "someurlref";[/I][/FONT] as I found, it wasn't until I changed the method that it worked, so its a good idea to keep that in mind.[/quote]Do you have an example of a current browser that doesn't. ISTR Opera 5.* didn't.
Copy linkTweet thisAlerts:
@JunkMaleOct 29.2011 — 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?
×

Success!

Help @ian9912 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...