PDA

View Full Version : Strange Javascript



hansan
06-20-2010, 12:47 PM
Hello javascript people,


I have designed a site. It's working properly in locally.


Now I want to insert a special javascript on my home page.


The job of that special javascript is, makes some errors in the site when site is going to publish on internet.

The site should run on locally but not on hosting

And I have found some javascript. but it's doesn't work, :confused:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>test</title>
<script type="text/javascript">
var myURL = window.location.href;
var patt1 = /localhost/i;
</script>
</head>
<body>
<script type="text/javascript">
if(patt1.test(myURL) || myURL.indexOf("http://localhost") != -1 || myURL.indexOf("file://") != -1 ) {

//code goes here

}

else {
throw("Execution on remote hosts is disabled for some reason");
}
</script>
</body>
</html>

can any one tell me what is the wrong with this code???

thnax.

molendijk
06-20-2010, 01:36 PM
The following script in the head:

<script type="text/javascript">
if(document.URL.substring(0,7) == "http://"){document.write("Execution on remote hosts is disabled for some reason")}
</script>
===
Arie Molendijk.

hansan
06-20-2010, 04:32 PM
Molendijk, thanx for responding me.
is this the way it use???

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>test</title>
<script type="text/javascript">
if(document.URL.substring(0,7) == "http://"){document.write("Execution on remote hosts is disabled for some reason")}

var myURL = window.location.href;
var patt1 = /localhost/i;
</script>

</head>
<body>
<script type="text/javascript">
if(patt1.test(myURL) || myURL.indexOf("http://localhost") != -1 || myURL.indexOf("file://") != -1 ) {

This is a testing

}

else {
throw("Execution on remote hosts is disabled for some reason");
}
</script>
</body>
</html>
in locally it should appear as "This is a testing". but in here not appear any thing even in the locally.
can you tell me what is the mistake i have made? how can it be used???

.

molendijk
06-20-2010, 08:19 PM
Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>test</title>
<script type="text/javascript">
onload=function(){
if(document.URL.substring(0,7) == "http://"){document.write("Execution on remote hosts is disabled for some reason")}
else {document.getElementById('local_testing').innerHTML="This is local testing"}
}
</script>
</head>
<body>
<div id="local_testing"></div>
</body>
</html>
Arie.

hansan
06-21-2010, 11:47 AM
molendijk, I wanted to test that code. that is why i put in there "This is a testing". actually it not should be a litter sting, it should be a several codes.

thanx for your suggestion :).
but i really want is make my site working on locally but not on hosting, as i mention in my 1st post.

so i want to put in this javascript in my index page. there are big codes goes on header part as well as body part. so your last suggestion is not good enough.

I think you have clear idea about my requirement.If you can help me it will be great help for me.

thank you

molendijk
06-21-2010, 01:34 PM
Hansan, I know what you want. The else-part in my code was just to give you the idea.
Here's an example with real code (second script) to be executed if you are not on the Internet. The first script is fired when you are on the Internet: it erases the page, then inserts the message. Just an example
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>test</title>
<script type="text/javascript">
onload=function(){
if(document.URL.substring(0,7) == "http://"){document.write("Execution on remote hosts is disabled.")}
}
</script>

<script type="text/javascript">
function red_background()
{
document.body.style.background='red'
}
</script>

</head>
<body>
<a href="javascript: red_background()">red background</a>

</body>
</html>
Arie.

hansan
06-22-2010, 02:09 PM
wow!!! :D this is the code i was looking for. thank you.
but still the code is not 100% perfect.
i.e the code can be test through a server (like wamp server). this code not allow to that. so we should put in that also.
testing on local machine via "http://localhost" or "127.0.0.1".

Is this correct ???

<script type="text/javascript">
onload=function(){
if((document.URL.substring(0,7) == "http://")||(document.URL.substring(0,7) == "http://localhost")){document.write("Execution on remote hosts is disabled.")}
}
</script>

you help me 95%. please help me other 5% also.:)

thanx

molendijk
06-22-2010, 04:22 PM
I'm not familiar with WAMP. It seems you want to say if the URL starts with http:// OR if it starts with http://localhost, then write "Execution on remote hosts is disabled".. But the part after OR is superfluous, since it starts with http://.
So I guess you want to say: if the URL starts with http:// then if it doesn't start with http://localhost, then write "Execution on remote hosts is disabled".
If I'm right about that, then you should write:

onload=function(){
if(document.URL.substring(0,7)=="http://" && document.URL.substring(0,16)!="http://localhost"){document.write("Execution on remote hosts is disabled.")}
}
===
Arie

hansan
06-22-2010, 07:11 PM
Yes, it's working perfectly molendijk.
thank you very much. i'm a beginner to javascript. so hope you will help me in yhe future also.

thank you again. :)