This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-03-04 14:54:03

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

I need help and I don't even know what language to use!

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:

Code:

<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'.  wink

________________________________________________________________________________


Edit: ...Wow. I got one that works. Thanks nXIII!

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>

Last edited by PullJosh (2012-03-09 19:27:11)


http://www.blocks.scratchr.org/API.php?action=text&amp;string=I'm_on_vacation!&amp;xpos=155&amp;ypos=90&amp;font_size=30&amp;bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#2 2012-03-04 14:59:40

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: I need help and I don't even know what language to use!

You need:

Code:

<input type="button" value="GO!" id="go" onclick="window.location = document.getElementById('Username').value;" /><br />

http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#3 2012-03-04 15:17:20

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: I need help and I don't even know what language to use!

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 />

I support the Free Software Foundation. Protect our digital rights!

Offline

 

#4 2012-03-04 15:34:05

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: I need help and I don't even know what language to use!

Yo dawg...

Just had to add this.


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#5 2012-03-04 15:44:47

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: I need help and I don't even know what language to use!

Fine, I'll validate it.


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#6 2012-03-04 16:00:20

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: I need help and I don't even know what language to use!

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>

nXIII

Offline

 

#7 2012-03-04 16:21:59

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: I need help and I don't even know what language to use!

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.


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#8 2012-03-04 16:29:10

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: I need help and I don't even know what language to use!

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?


http://i.imgur.com/zeIZW.png

Offline

 

#9 2012-03-04 16:55:20

spud2451
Scratcher
Registered: 2010-05-01
Posts: 100+

Re: I need help and I don't even know what language to use!

here's some code i whipped up that will tell the person what their page url is

Code:

<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>

http://www.xenopages.comze.com/upload/rippleos.gif

Offline

 

#10 2012-03-04 17:35:55

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: I need help and I don't even know what language to use!

Looks good at first glance.
Although in this case, the form is not necessary. If it's just JS, there is no need for a form.


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#11 2012-03-04 19:20:53

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: I need help and I don't even know what language to use!

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


nXIII

Offline

 

#12 2012-03-05 00:48:01

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: I need help and I don't even know what language to use!

You can use PHP and HTML forms.

HTML code

Code:

<form action="navigate.php" method="post">
Enter your username:<input="text" name="username"/>
</form>

PHP code "navigate.php"

Code:

$username = $_POST["username"];
header( 'Location: http://scratch.mit.edu/users/'.$username.' ' ) ;

smile

Last edited by jji7skyline (2012-03-09 01:48:48)


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#13 2012-03-05 02:49:51

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: I need help and I don't even know what language to use!

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  smile

@jji7skyline it would be <form action='navigate.php' method='post'>


http://i.imgur.com/zeIZW.png

Offline

 

#14 2012-03-05 12:07:56

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: I need help and I don't even know what language to use!

PullJosh wrote:

CSS STARTS HERE...

?


http://i.imgur.com/zeIZW.png

Offline

 

#15 2012-03-08 16:32:20

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: I need help and I don't even know what language to use!

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.


http://i.imgur.com/zeIZW.png

Offline

 

#16 2012-03-20 20:27:17

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

Re: I need help and I don't even know what language to use!

Could this be closed? Thanks!


http://www.blocks.scratchr.org/API.php?action=text&amp;string=I'm_on_vacation!&amp;xpos=155&amp;ypos=90&amp;font_size=30&amp;bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#17 2012-03-20 21:09:03

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: I need help and I don't even know what language to use!

PullJosh wrote:

Could this be closed? Thanks!

If you want this thread to be closed, click the "Report" button and explain in the description.   smile

Offline

 

#18 2012-03-21 07:22:59

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

Re: I need help and I don't even know what language to use!

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.   smile

OK. I just wasn't quite sure. Thanks for the help!  smile


http://www.blocks.scratchr.org/API.php?action=text&amp;string=I'm_on_vacation!&amp;xpos=155&amp;ypos=90&amp;font_size=30&amp;bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#19 2012-03-21 07:26:49

cheddargirl
Scratch Team
Registered: 2008-09-15
Posts: 1000+

Re: I need help and I don't even know what language to use!

Closed by request.


http://i.imgur.com/8QRYx.png
Everything is better when you add a little cheddar, because when you have cheese your life is at ease  smile

Offline

 

Board footer