Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open page in new tab using javascript window.open(elementName.elementValue) in anchor tag

I've examined tons of examples over the past couple days, but still can't find an answer to my dilemma. In order for me to explain my dilemma first I'll show you an example of something I have that works, then explain how I would rather it work (but can't seem to achieve).

This works:

<form>
<select name="url">
    <option selected="selected" value=""> Choose Donation Amount </option>
    <option value="http://www.url1.com">URL#1</option>
    <option value="http://www.url2.com">URL#2</option>
    <option value="http://www.url3.com">URL#3</option>
    <option value="http://www.url4.com">URL#4</option>
    <option value="http://www.url5.com">URL#5</option>
    <option value="http://www.url6.com">URL#6</option>
</select>
<input type="submit" value="Submit" onclick="if (url.value) window.open(url.value);" />
</form>

But what I'd rather do is capture the option and open the URL using an anchor tag (rather than input[type"submit"]), something like this (to replace the input tag before the closing form tag):

<a href="javascript:void(if (url.value) window.open(url.value));" target="_blank">Submit</a>

The above line doesn't work, and I can't figure out how to form it correctly. Help?

I know this doesn't seem logical, especially since I already have it working using a submit button, but it's also not practical for me to explain exactly why I don't want to use input or button type="submit", , or PHP. Please, it really needs to be inline javascript in an anchor tag. Thanks.s :-)

like image 518
Jeff Avatar asked Apr 15 '12 22:04

Jeff


People also ask

How do I open an anchor in a new tab?

You just need an anchor ( <a> ) element with three important attributes: The href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.

How do I open a new tab with JavaScript?

To open a new tab, we have to use _blank in the second parameter of the window. open() method. The return value of window. open() is a reference to the newly created window or tab or null if it failed.

How can you open a link in a new tab browser window?

To open a link in a new tab, click the link by pressing down your middle mouse button, or right-click the link and select Open link in New Tab. If your mouse has a wheel, it can be used as a button if you press down on the wheel. These methods work in all of the major Internet browsers available for Microsoft Windows.

How do you make a link open in a new tab HTML?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.


1 Answers

Add your form name attribute like:

<form name="form">

... and then try it like that:

<a href="" onclick="window.open( form.url.value, 'windowName' ); return false" target="_blank">Submit</a>

There is a working example.

like image 93
PrimosK Avatar answered Sep 24 '22 20:09

PrimosK