admin管理员组

文章数量:1392007

I have a little doubt about javascript, a new language that I'm studying...

I have a variables that contains a link, like

var Link = ";k=&e=1";

I'd like to press a button and go to the link, that I've saved in a the variable Link. I've tryed with:

<a href=Link><input type="submit" value="Go" /></a>

What's wrong?

PS

The link I want to edit is not the link of my page (I can't use this.document.location.href). My button should redirect the user to another website and do a custom search.

PPS If you want an example. I have a textbox on my website. If the user write something in my textbox and clicks the Go button the text in my textbox will replace JAVASCRIPTVARIABLE in =&s_it=search51&q=JAVASCRIPTVARIABLE and will open the AOL research page.

I have a little doubt about javascript, a new language that I'm studying...

I have a variables that contains a link, like

var Link = "http://www.example./search?q=javascriptvariable&k=&e=1";

I'd like to press a button and go to the link, that I've saved in a the variable Link. I've tryed with:

<a href=Link><input type="submit" value="Go" /></a>

What's wrong?

PS

The link I want to edit is not the link of my page (I can't use this.document.location.href). My button should redirect the user to another website and do a custom search.

PPS If you want an example. I have a textbox on my website. If the user write something in my textbox and clicks the Go button the text in my textbox will replace JAVASCRIPTVARIABLE in http://search.aol./aol/search?enabled_terms=&s_it=search51&q=JAVASCRIPTVARIABLE and will open the AOL research page.

Share Improve this question edited Jun 15, 2012 at 14:41 user1453638 asked Jun 15, 2012 at 14:16 user1453638user1453638 1412 gold badges3 silver badges11 bronze badges 5
  • How are you outputting that HTML? – T.J. Crowder Commented Jun 15, 2012 at 14:18
  • 1 You cannot use JavaScript variables as values for HTML attributes like this. Why do you think you can? You should read more introductory material. Also, having a button inside a link is very odd. – Felix Kling Commented Jun 15, 2012 at 14:19
  • @FelixKling keep in mind the OP said they are learning. Be helpful rather than judgmental. – KP. Commented Jun 15, 2012 at 14:25
  • @user1453638 your edit / intended purpose sounds a little suspect? – KP. Commented Jun 15, 2012 at 14:26
  • Why can't you use document.location.href? It allows you to do everything a link can. – trumank Commented Jun 15, 2012 at 14:28
Add a ment  | 

5 Answers 5

Reset to default 2

Give the link an ID first,

<a id="someLink"><input type="submit" value="Go" /></a>

Then, in your javascript you will need

var Link = "http://www.example./search?q=javascriptvariable&k=&e=1";
document.getElementById('someLink').href = Link;

Make your link

<input type="button" value="Go" onclick="document.location= Link;" />

You need to set it with JavaScript. It is also better to use a button.

HTML:

<button id="go">Go!</button>

​ JavaScript:

var Link = 'http://jsfiddle'

document.getElementById('go').onclick = function () {
    document.location= Link;
};​

Working example: http://jsfiddle/PfWdW/

<input type="button" value="Go" onclick="location.href=Link;" />

Just noting I wouldn't remend this type of approach.. but to answer it directly the above works. Why not just use a normal link without script?

Please try this code:

<input type="button" value="Go" onclick="window.location.href='http://www.example./search?q=javascriptvariable&k=&e=1'" />

本文标签: htmlJavascript and a href with variablesStack Overflow