Pages: 1
Topic closed
I'm making a site for new scratchers. On the page about the scratch website, I want to make it so that they type in their username and it will take them to their page. The way this should work is scratch.mit.edu/users/UsernameHere. When they hit go, it would automatically link (window.location()) to their page.
This is the html:
<input type="text" id="Username" /> <input type="button" value="GO!" id="go" /><br />
If you think I might have even a little more help, I got nothin'.
________________________________________________________________________________
Edit: ...Wow. I got one that works. Thanks nXIII!
<input id=myStuffUsername type=text placholder=Username>
<a id=myStuffLink href=#>My Stuff</a>
<!-- ... -->
<script>
document.querySelector('#myStuffLink').addEventListener('click', function () {
window.location = 'http://scratch.mit.edu/users/' +
encodeURIComponent(document.querySelector('#myStuffUsername').value);
}, false);
</script>Last edited by PullJosh (2012-03-09 19:27:11)

Offline
jvvg wrote:
You need:
Code:
<input type="button" value="GO!" id="go" onclick="window.location = document.getElementById('Username').value;" /><br />
Even better:
<input type="button" value="GO!" id="go" onclick="window.location = "http://scratch.mit.edu/users/" + document.getElementById('Username').value;" /><br />Offline
Offline
Fine, I'll validate it.
Offline
As MathWizz pointed out, you should pretty much never use inline event handlers; I would recommend something like this:
<input id=myStuffUsername type=text placholder=Username>
<a id=myStuffLink href=#>My Stuff</a>
<!-- ... -->
<script>
document.querySelector('#myStuffLink').addEventListener('click', function () {
window.location = 'http://scratch.mit.edu/users/' +
encodeURIComponent(document.querySelector('#myStuffUsername').value);
}, false);
</script>Offline
bobbybee wrote:
jvvg wrote:
You need:
Code:
<input type="button" value="GO!" id="go" onclick="window.location = document.getElementById('Username').value;" /><br />Even better:
Code:
<input type="button" value="GO!" id="go" onclick="window.location = "http://scratch.mit.edu/users/" + document.getElementById('Username').value;" /><br />
Oops, guess I was typing too quickly.
Offline
nXIII wrote:
As MathWizz pointed out, you should pretty much never use inline event handlers; I would recommend something like this:
Code:
<input id=myStuffUsername type=text placholder=Username> <a id=myStuffLink href=#>My Stuff</a> <!-- ... --> <script> document.querySelector('#myStuffLink').addEventListener('click', function () { window.location = 'http://scratch.mit.edu/users/' + encodeURIComponent(document.querySelector('#myStuffUsername').value); }, false); </script>
I was just wondering what the benefits of doing this/cons of using inline event handlers are?
Offline
here's some code i whipped up that will tell the person what their page url is
<html>
<head>
<script type="text/javascript">
var username
var url
function get_page(){
username = document.form1.usernamebox.value;
url = 'http://scratch.mit.edu/users/' + username ;
alert("your scratch page is " + url );
}
</script>
</head>
<body>
<form name="form1">
Scratch Username:<br>
<input type="text" value="userame" name="usernamebox">
<br><br>
<input type="button" value="Get Page!" onclick="get_page();">
</form>
</body>
</html>Offline
rookwood101 wrote:
I was just wondering what the benefits of doing this/cons of using inline event handlers are?
There are a couple of reasons:
- This helps separate the content from the behavior, which helps you write more maintainable and organized code
- HTML is not cached, so inline JS is not cached (my <script> should really have been in a separate .js file)
- As MathWizz pointed out, inline event handlers are really eval() in disguise; this is bad because:
- eval can be a security risk (not really applicable here, but it's a good habit to not use eval unless you really have to)
- eval kills JS engine optimizations, because you're executing unknown code at runtime
Offline
You can use PHP and HTML forms.
HTML code
<form action="navigate.php" method="post"> Enter your username:<input="text" name="username"/> </form>
PHP code "navigate.php"
$username = $_POST["username"]; header( 'Location: http://scratch.mit.edu/users/'.$username.' ' ) ;
Last edited by jji7skyline (2012-03-09 01:48:48)
Offline
nXIII wrote:
rookwood101 wrote:
I was just wondering what the benefits of doing this/cons of using inline event handlers are?
There are a couple of reasons:
- This helps separate the content from the behavior, which helps you write more maintainable and organized code
- HTML is not cached, so inline JS is not cached (my <script> should really have been in a separate .js file)
- As MathWizz pointed out, inline event handlers are really eval() in disguise; this is bad because:
- eval can be a security risk (not really applicable here, but it's a good habit to not use eval unless you really have to)
- eval kills JS engine optimizations, because you're executing unknown code at runtime
Ah okay, I didn't really think about them being eval, but that makes sense, thanks
@jji7skyline it would be <form action='navigate.php' method='post'>
Offline
PullJosh wrote:
*Knocking*... "Anyone home?"...
You can't really just ask for css. It all depends on what you want the site to look like, and what there is on the site.
Offline
PullJosh wrote:
Could this be closed? Thanks!
If you want this thread to be closed, click the "Report" button and explain in the description.

Offline
Greenatic wrote:
PullJosh wrote:
Could this be closed? Thanks!
If you want this thread to be closed, click the "Report" button and explain in the description.
![]()
OK. I just wasn't quite sure. Thanks for the help!

Offline
Closed by request.

Offline
Topic closed
Pages: 1