I found this code on jsfiddle and it works fine, inside of fiddle. But when I try to put it inside of my own HTML file I can't get it to work. I am sure I left something out of the script tag, but I just don't know what. Any input would be greatly appreciated.
Here is the fiddle: Working Fiddle
PS The code is supposed to pull info from the URL and fill the form.
-Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Parser</title>
</head>
<body>
<p>Your first name:
<br /><input name="name (awf_first)" id="name (awf_first)" /></p>
<p>Your last name:
<br /><input name="name (awf_last)" id="name (awf_last)" /></p>
<p>Your email:
<br /><input name="email" id="email" /></p>
<p>Your password:
<br /><input name="password" id="password" /></p>
<script>
var hashParams = window.location.hash.substr(1).split('&'); // substr(1) to
remove the `?`
for(var i = 0; i < hashParams.length; i++){
var p = hashParams[i].split('=');
document.getElementById(p[0]).value = decodeURIComponent(p[1]);;
}
</script>
</body>
</html>
Your error means that document.getElementById(p[0])
is not finding the #name
and #email
input elements.
This could be for two reasons:
id
s of: name
and email
. Check your elements to make sure that you have given them correct id
s. Your code shows elements with id
s of: name (awf_first)
and name (awf_last)
, when the id
s should be: name
and email
.
Make sure that the script
comes AFTER the bulk of the HTML (just before the close of the body
tag) as it is shown in the Fiddle, so that by the time you run that line, the element(s) will already be in the DOM.
Also, be aware that in your question, the line // substr(1) to remove the '?'
is split across two lines and is currently causing an error in your code. And, within that comment change the back-ticks surrounding the ?
with single quotes.
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.