Passing information using the DOM between HTML files and linked .js files.
Can someone tell me or show me 开发者_JS百科how to pass information from a HTML page to a linked .js file?
<head>
<script type="text/javascript" src="../javascripts/script.js"></script>
</head>
<form name="form1">
<input type="text" name="input1" value autofocus="on">
<button onClick= "chrome.tabs.create({url: getURL()});">Go</button>
</form>
This is part of a form I want to be completed, and the input value taken, combined with a pre-existing URL, and the end result opened in a new tab
function getURL()
{
var site = "http://www.example.com/query?f=";
var input = document.form1.input1.value
var output = site + input;
return output;
}
This is the function I'll be using, but I can't work out or don't know yet how to link the input variable to the input1 form value. The .js file is linked, rather than inside the actual HTML file.
Help?
Elements with their IDs or names automatically mapped to hierarchically-organised properties under document
, as in document.name1.name2
, is a long-deprecated practice and doesn't occur unless your document triggers quirks mode. Add an ID to the input element then use
var input = document.getElementById('input_id_goes_here').value;
instead.
精彩评论