Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement; getting the window URL

Status
Not open for further replies.

Davehead

Programmer
Sep 22, 2005
3
GB
Hi all.
What I am trying to do, is get the current pages URL, and then using that to determine what content (held in a variable in the external .js) will be displayed.

I have created the following in an external JS file, just to purely see whether the function is working or not:

function displayCategory() {
if (window.location=" {
document.write ("Internet Connection")
}
else {
document.write ("Mobiles")
}
}

now, in the page where I want to call the function, i have :
<a href="javascript:void(0)" onclick="displayCategory()">link</a>

So, above is obviously just a basic check, BUT either way, I only ever get "Internet Connection" written to the document. Is it my if/Else statement no working? What would be the best way to go about:
A)determining the current page URL and
B) Running a function/displaying a variable based on the url.

PLease help somebody.
D
 
Ahhh... I had this one stump me at a job interview once!

You need to use == (not =) for a test:
Code:
if (window.location=[COLOR=red][b]=[/b][/color]"[URL unfurl="true"]http://test.getcheaper.co.uk/products/Internet+connection/")[/URL] {
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Hey thanks for the replies.
BabyJeffy : always the smallest things eh? :)
tgreer: where would I need to use the "document.close" and why would it be used in this case?

Also, I have now got my initial question sorted (thanks again)
So, I now have (external .js):

function displayLinks() {
if (window.location==" {
var text = "Internet Connection"
}
else if (window.location==" {
var text = "Mobiles"
}
}

How could I get "var text" to be displayed in a <td> on my page. document.write obviously, but I have a feeling the code is wrong ???? :-S
 
Whenever you use the document.write() statement, you are working with an OPEN document. The "open" is implicit. The "close" is not. If you don't use document.close(), the browser will appear to be "waiting" or in a "constantly running" state.

In your revised posting, you aren't using document.write(), so no document.close() is required.



Thomas D. Greer
 
OK, thanks for that, and, sorry to be a pain, but how could i display var text in my page ???
Sorry, bit of a js newbie
Cheers
 
It depends on what DOCTYPE you're using. In some more recent HTML/XHTML versions, document.write() is deprecated. The canonical way is to set the relevant property of the particular element. For example, by using the "innerHTML" property of a DIV element.

Thomas D. Greer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top