I'm having a hard time getting a submit button to open a link in a new window.
The button code:
<input type="button" name="buy" value="Buy" onClick="parent.location='myurl'" />
I tried to add a target="_blank" to the form, but that didn't help.
Is it possible to open a new window using this "parent.location" method or any equivalent.
I'm afraid my shopping cart script won't work any longer if I change the code too much.
Best regards,
Erik Chan
<form action="whatever" method="post" target="foo" onSubmit="window.open('', 'foo', 'width=1040,height=900,status=yes,resizable=yes,scrollbars=yes')">
This should work for your needs. I have used this time and time again
<input type="button" name="buy" value="Buy" onClick='window.open("http://www.abcd.com/")' />
see it for open function param
http://www.w3schools.com/jsref/met_win_open.asp
Try window.open() method.
<input type="button" name="buy" value="Buy" onclick="window.open('myurl')">
Related
Todo :
Close a form by clicking a button. Any solution because I tried more and I saw more method but no result. None of these methods work. any idea.
<form method="post">
<input type="button" value="Close Window"
onclick="window.close()">
</form>
<input type="button" name="cancelvalue" value="CANCEL"
onClick="self.close()">
If you want the window to get closed by javascript, then javascript must have opened that window in the first place to close it.
But, there is some hack using "self redirection", And the following will work easily open(location, '_self').close();
<form method="post">
<input type="button" value="Close Window"
onclick="open(location, '_self').close();">
</form>
<input type="button" name="cancelvalue" value="CANCEL"
onClick="open(location, '_self').close();">
I'm using a javascript search engine from this script page that allows me to select different providers/search engines to perform a google like search, but I'm trying to make it open in a new window after I press the send button, without success.
I've already tried to use:
target="_blank"
in <input type="button" value="Send" onClick="startSearch()" target="_blank"> and nothing happens, or:
<form name="searchForm" target="_blank">
Nothing!
I've also tried to use _new instead of _blank and didn't work either.
Finally, I've tested what suggested on w3schools.com example but unfortunately again a failure.
Can you please, guys, help? Thanks!
try something this if you want to open a new tab link by your button-
<a href="http://www.stackoverflow.com/" target="_blank">
<input type="button" value="Send" />
</a>
Quick and dirty, you can use the window.open(); method to open the search in a new window, here's some updated HTML:
<form method="GET" action="#" id="search-form">
<p><label>Search for: <input type="text" id="q" /></label></p>
<p><label>Search from: <select id="engine">
<option selected="selected" value="http://www.google.com/search?q=">Google</option>
<option value="http://www.bing.com/search?q=">Bing</option>
<option value="http://search.yahoo.com/search?p=">Yahoo!</option>
<option value="http://search.aol.com/aol/search?q=">AOL</option>
</select></label></p>
<p><input type="button" value="Send" id="send" /></p>
</form>
And the JavaScript:
<script>
var ID = function(str){return(document.getElementById(str));};
ID('send').addEventListener('click', function(){
var search = ID('q').value, select = ID('engine'), engine = select.options[select.selectedIndex].value;
if(!search) {
alert('Please include a search string');
} else {
window.open(engine + search);
}
});
</script>
Just make sure to call that at the end of the page or put it in a DOMContentLoaded event or something.
MDN Docs on window.open()
To open the link in a new window, since you are using a <form> instead o an <a> link, you have to change the javascript shown in the example, first adding an ID to the form so it is easilly accessible via the DOM:
<form id="searchForm" name="searchForm" target="blank">
Then, change the last javascript line so instead of the location.href, it changes the form's action and submits it:
document.getElementById("searchForm").action = finalSearchString;
document.getElementById("searchForm").submit();
Example:
https://jsfiddle.net/vj99ux9e/
im completely new to html && javascript coding so now im doing my first steps and trying to write a search bar which will pass google your input text
<script src="func.js"></script>
<form name="googleSeach">
<p align="center"><input name="searchTxt" id="searchTxt" type=search placeholder="Google Search">
<input type="submit" value="Find" onclick="test()"></p>
</form>
and javascript
function test(){
window.open("https://www.google.ru/webhp?newwindow=#q="+document.getElementById('searchTxt').value,"_self")
}
well, thought it should work but no, it does not. What's the proble?
Believe you just want to use
<form name="googleSeach" onsubmit="test(event)">
Instead of onclick.
Also your handler should probably cancel the submit action. Also use location.href instead of window.open since you're trying to open the new URL in the same window.
http://jsfiddle.net/bvaughn/fr35mpgf/2/
I am using below code to generate buttons with link. but problem is it is opening in same tab. i want it to open in new tab. Can somebody suggest me a method which will work for most of the browsers.
<form>
<input TYPE="button" VALUE="Home Page"
onclick="window.location.href='http://www.wherever.com'">
</form>
Also kindly suggest if we have any better method to do this.
Thanks in advance.
Since this is javascript you need to use like this:
<form>
<input TYPE="button" VALUE="Home Page"
onclick="window.open('http://www.wherever.com');">
</form>
If you want to open it in a new tab, you have to set the target to _blank (as that it the target for a new tab).
<form>
<input TYPE="button" VALUE="Home Page"
onclick="window.open('http://www.example.com','_blank')">
</form>
I have a button in the jquery mobile form, which defines the post action attributes.
<form id="form1" enctype="multipart/form-data" data-ajax="false" method="POST" target="uploadTarget" action="https://graph.facebook.com/me/photos?access_token="...">
<input id="source" name="source" type="file">
<input id="message" name="message" type="text">
<button id="upload" type="submit" name="upload">Upload Photo</button>
</form>
The post method on the form is not called when I click on the styled button.
The method 'post' is called on the form if I use data-role="none"
<button data-role="none" id="upload" type="submit" name="upload">Upload Photo</button>
But the button obviously loses the mobile styles.
I have data-ajax="false" at the form level as shown (thanks to CBroe!) , but that does not help.
Is there any option to get the jquery mobile styles on buttons, but leave the default Form events and submit/post behavior.
I have already tried to data-ajax="false", but that does not help.
Where did you put that – on the button element, or on the form element? (It’s supposed to be set on the form.)
The problem was the mobile css file. I was using an older version. after including the latest version :
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile.min.css" />
It seems to work.