0

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.com/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.com/aol/search?enabled_terms=&s_it=comsearch51&q=JAVASCRIPTVARIABLE and will open the AOL research page.

5
  • How are you outputting that HTML? 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. Commented Jun 15, 2012 at 14:19
  • @FelixKling keep in mind the OP said they are learning. Be helpful rather than judgmental. Commented Jun 15, 2012 at 14:25
  • @user1453638 your edit / intended purpose sounds a little suspect? Commented Jun 15, 2012 at 14:26
  • Why can't you use document.location.href? It allows you to do everything a link can. Commented Jun 15, 2012 at 14:28

5 Answers 5

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.com/search?q=javascriptvariable&k=&e=1";
document.getElementById('someLink').href = Link;
Sign up to request clarification or add additional context in comments.

1 Comment

I think that's the best approach as the html code stays pure html and the coding is done separately.
1

Make your link

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

2 Comments

Really? I would suggest to remove the link completely, or the button, one of them.
buttons are ugly and hard to style - you could always add the link programatically but personally I prefer to do it this way so that I can see what my layout will look like
1

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.net'

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

Working example: http://jsfiddle.net/PfWdW/

1 Comment

That is true, but I don't think it's a very helpful answer.
0
<input type="button" value="Go" onclick="location.href=Link;" />

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

Comments

0

Please try this code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.