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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a variable in href tag?

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I use a browser based system of checking procedures, written in html and javascript.

I’m doing a new one where I have a few <select> tag options.
I want to use these with onchange functions, to build up a variable that contains a url, and use that variable in a link lower down the page.
I can do the onchange functions I’m sure, already using something like that, but I’m failing to get the link to work.

Done some googling and seemed to be straight forward, but the hits I found don’t seem to work for me.

Just to get going I had this on the head:
JavaScript:
<script>
<!--
function Page(){
  location.href="[URL unfurl="true"]www.google.com";[/URL]
}
// -->
</script>

Then in my <body>

I have:
HTML:
<td width="20"><a href="javascript:page()" target="new2" style=""><img src="Checking_Images/info_icon_red.jpg" border="0"></a></td>

All that happens is a blank tab opens, url is “about:blank”

I’m using firefox 13 on windows 7 64bit.

Where am I going wrong?


Steve (Delphi 2007 & XP)
 
javascript is case sensitive, "Page" and "page" are NOT interchangeable.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Oh and href is a attribute of an anchor opening tag NOT a tag itself.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
The usage of the javascript: protocol is considered a bad practice. That is placing an href that's not really a valid location is never a good idea.

Other than that, the target attribute is messing with the JS in that it opens the new page, but prevents the JS from actually running, since the function does not exist in the new opened page, but in the originating page only, so it can't be accessed.

If you want to open a new page you'll need to do it from the Javascript code rather that on the link itself.


Code:
[COLOR=#990000]<[/color]a href[COLOR=#990000]=[/color][COLOR=#FF0000]"#"[/color] onclick[COLOR=#990000]=[/color][COLOR=#FF0000]"return Page();"[/color][COLOR=#990000]><[/color]img src[COLOR=#990000]=[/color][COLOR=#FF0000]"Checking_Images/info_icon_red.jpg"[/color] border[COLOR=#990000]=[/color][COLOR=#FF0000]"0"[/color][COLOR=#990000]></[/color]a[COLOR=#990000]>[/color]
Code:
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]Page[/color][/b][COLOR=#990000]()[/color][COLOR=#FF0000]{[/color]
  window[COLOR=#990000].[/color][b][COLOR=#000000]open[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"www.google.com"[/color][COLOR=#990000],[/color][COLOR=#FF0000]"new2"[/color][COLOR=#990000]);[/color]
  return [red]false[/red];
[COLOR=#FF0000]}[/color]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
schoolboy error corrected, and anchor semantics noted, thanks.

Corrected case, still the same.

Steve (Delphi 2007 & XP)
 
ah vacunita you posted while I was replying.

That looks good. I'll try that right now thanks.
I was wondering after I posted if the target might be an issue.


Steve (Delphi 2007 & XP)
 
sorted. Many thanks Phil!

Steve (Delphi 2007 & XP)
 
Glad it worked for out.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
CADTenchy said:
schoolboy error corrected, and anchor semantics noted, thanks.

Wow! I'm impressed :)

Usually when I correct things like that, some take umbrage at it as being a personal insult to their intelligence.

Which of course it is not intended as such. So thank you, for restoring the balance somewhat.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Wow! I'm impressed smile

Usually when I correct things like that, some take umbrage at it as being a personal insult to their intelligence.

Which of course it is not intended as such. So thank you, for restoring the balance somewhat.

If I felt I was in the right I would argue to the death :)

However, what umbrage can I take from being accurately corrected? :)

Steve (Delphi 2007 & XP)
 
CADTenchy said:
However, what umbrage can I take from being accurately corrected?
Oooh! you would be surprised (or maybe not) :)

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I was on usenet, in unmoderated newsgroups.
I don't there is anything that could surprise or shock me any more [dazed]

Steve (Delphi 2007 & XP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top