Results 1 to 9 of 9

Thread: Strange Javascript

  1. #1
    Join Date
    May 2010
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Angry Strange Javascript

    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,

    Code:
    <!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.
    Last edited by hansan; 06-20-2010 at 12:53 PM.

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    The following script in the head:
    Code:
    <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.

  3. #3
    Join Date
    May 2010
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Molendijk, thanx for responding me.
    is this the way it use???
    Code:
    <!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???

    .
    Last edited by hansan; 06-20-2010 at 05:03 PM.

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Try this:
    Code:
    <!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.
    Last edited by molendijk; 06-20-2010 at 08:24 PM. Reason: Correction

  5. #5
    Join Date
    May 2010
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    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
    Code:
    <!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.

  7. #7
    Join Date
    May 2010
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    wow!!! 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 ???
    Code:
    <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

  8. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

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

  9. The Following User Says Thank You to molendijk For This Useful Post:

    hansan (06-22-2010)

  10. #9
    Join Date
    May 2010
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •