简体   繁体   中英

Display Results of Form Submission on New Tab or Window

There are several variations of this question already out there but I don't see the solution to this one specifically. Although I don't actually need this functionality it is killing me that I CAN'T make it work!

I have a field that allows the visitor to type in a URL and it will take them there.

<form name="urlField" onsubmit="return submitURLFieldForm();">
  <input type="text" name="address" id="addressfield" />
</form>

The JS that handles this so that the necessary protocol is not left off is (special thanks to @ h2ooooooo ):

function submitURLFieldForm() {
var url = document.getElementById('addressfield').value;
if (!url.match(/^[a-zA-Z]+:\/\//)) {
    url = 'http://' + url;
}

window.location.href = url; 
return false;
}

If I add a target="_blank" to my opening form tag it doesn't work. Why? And where should I add it?

You are never submitting the form.

You have an event handler that, when the form starts to submit, sets location and prevents the normal form submission.

Use window.open(url) instead of that if you want to open a new window.

var win=window.open(url, '_blank');
win.focus();

You can try this :)

happy coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM