Javascript: shouldn't this code work?

The code below is one i made, and i’m trying to do this:
http://hotwired.lycos.com/webmonkey/98/04/index1a_page15.html?tw=programming

Click on ‘heading’ and you’ll see. The structure of this seems to work for me, but somehow it doesn’t seem to work? Anyone know why? THe source code is much different from mine, and i don’t understand it much, so i decided to check my code here :slight_smile: thanks


<script language="JavaScript">
var the_url = prompt("Which website now?", "")
var finUrl = adjust_url()
var new_window = window.open(finUrl, "opening_website")
function adjust_url() {
var checkUrl = the_url.substring(0,7)
if (checkUrl != "http://") {
the_url = "http://"+the_url
}
}
</script>

Anyone have any clue? :-\

honestly I am terrible at Jav**ascript. Lost has been gone for a couple days, when he comes back I will make sure he takes a look at this :slight_smile: He might know…

<script language="JavaScript">
var the_url = prompt("Which website now?", "");
var finUrl = adjust_url();
var new_window = window.open(finUrl, "opening_website");
function adjust_url() {
	var checkUrl = the_url.substring(0, 7);
	if (checkUrl != "http://") {
		the_url = "http://"+the_url;
	}
	return the_url;
}
</script>

ah thanks kax, never knew you knew javascript :slight_smile: anyways i just have a few questions :stuck_out_tongue:

what does return the_url do??

I also wanted to make my thing exactly the same as the site i showed you (so the prompt only opens when you click a link). I copied this part from the source, but i don’t understand it. Would you mind explainin it to me? :slight_smile:


function goThere()
{
	var the_url = prompt("where can I take you?", ""); 
	if (the_url)
	{
		var good_url = fixURL(the_url);
		var new_window = open(good_url,"new_window","menubar,resizeable");
	}
}

What i don’t get is mainly the:

if (the_url)…I don’t see any thing it is comparing to, to make the if function work.

And last question :slight_smile:

function fixURL(the_url)

that line. Its only the beginning of a function, but what do the things in the brackets of the functions do? thankks!

I wouldn’t say I know JavaScript. I just gave it a shot. :stuck_out_tongue:

Anyway, isn’t the tutorial supposed to teach you that?
I’ll read it later and try to explain it to you. I won’t promise anything, though. :whistle:

eh ok :slight_smile: well it is suppose to teach me. But then it gives me exercises and i havta figure em out myself, but as you can see, its not workin too well :stuck_out_tongue:

I could explain it to you but your missing a part of it. There is a fixURL function that is called in that but you don’t show any reference to it in the above source. Post that and I shall explain.

ooh yah. So heres the whole thing source from the site:


<script language="JavaScript">
<!-- hide me

function goThere()
{
	var the_url = prompt("where can I take you?", ""); 
	if (the_url)
	{
		var good_url = fixURL(the_url);
		var new_window = open(good_url,"new_window","menubar,resizeable");
	}
}

function fixURL(the_url)
{
	var the_first_seven = the_url.substring(0,7);
	the_first_seven = the_first_seven.toLowerCase();
	if (the_first_seven != 'http://') 
	{
		the_url = "http://" + the_url;
	}
	return the_url;
}

// show me -->
</script>

I just need the parts i said in the above post explained. Thanks :slight_smile:
Oh yea, there is also a line which calls the goThere() function, but i don’t think that one is important because its just a link.

What it does is take what you are typing into the prompt. In this case it would be something like http://www.google.com or just www.google.com.

This is the part that prompts you:


var the_url = prompt("where can I take you?", ""); 

Now this code


if (the_url)
    {
        var good_url = fixURL(the_url);
        var new_window = open(good_url,"new_window","menubar,resizeable");
    }


Where it says “if (the_url)” it is asking if the_url is true or there is something in the variable at all in this case then go inside and run the function fixURL on the_Url.

Now in fixURL, what it does is check the first 7 characters of the string to see if they are http://. If they aren’t then it adds http:// to the front of the url ( the_url = “http://” + the_url;).

Then going back up to the goThere() function it pops open a new window using your “fixed” url.

That should explain it but post any questions you have blah.
:p:

ah yeea that cleared a bit up. BUt i am just wondering, why is it:

fixURL(the_url) and not just fixURL()?

I’m a bit confused with that :-\

And also the return the_url. I’m familiar with actionscript, and they never needed any of this crap :stuck_out_tongue: So what exactly does it do? thanks pimp!! :wink:

It is fixURL(the_url) because you are passing the_url into the fixURL function so that fixURL can manipulate the string that was inputed by the user.

I’m not sure if your asking a quesiton or not in that last paragraph. If so, then please clarify…

Yea thae last paragraph was a question…just wanted to know what return the_url does, and when/why you havta use it :slight_smile: