# Javascript: shouldn't this code work?

**URL:** <https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102>\
**Category:** Uncategorized\
**Created:** [August 10, 2003, 3:53am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102 "2003-08-10T03:53:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![blah\_de\_blah](https://avatars.discourse-cdn.com/v4/letter/b/a8b319/32.png) [@blah\_de\_blah](https://forum.kirupa.com/u/blah_de_blah)\
**Post date:** [August 10, 2003, 3:53am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/1 "2003-08-10T03:53:57Z")

</div>

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](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 🙂 thanks

```php

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 6:02am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/2 "2003-08-11T06:02:03Z")

</div>

Anyone have any clue? :-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 6:07am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/3 "2003-08-11T06:07:22Z")

</div>

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 🙂 He might know…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 8:01am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/4 "2003-08-11T08:01:14Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 8:18am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/5 "2003-08-11T08:18:54Z")

</div>

ah thanks kax, never knew you knew javascript 🙂 anyways i just have a few questions 😛

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

```php

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 🙂

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 8:38am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/6 "2003-08-11T08:38:46Z")

</div>

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

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 8:58am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/7 "2003-08-11T08:58:23Z")

</div>

eh ok 🙂 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 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 7:37pm UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/8 "2003-08-11T19:37:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2003, 2:07am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/9 "2003-08-12T02:07:49Z")

</div>

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

```php

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2003, 2:43pm UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/10 "2003-08-12T14:43:33Z")

</div>

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

This is the part that prompts you:

```auto

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

```

Now this code

```auto

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2003, 2:59pm UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/11 "2003-08-12T14:59:57Z")

</div>

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 😛 So what exactly does it do? thanks pimp!! 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2003, 9:54pm UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/12 "2003-08-12T21:54:50Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 13, 2003, 2:34am UTC](https://forum.kirupa.com/t/javascript-shouldnt-this-code-work/28102/13 "2003-08-13T02:34:06Z")

</div>

Yea thae last paragraph was a question…just wanted to know what return the\_url does, and when/why you havta use it 🙂
