JavaScript DHTML/Javascript Objects/location

Материал из Web эксперт
Перейти к: навигация, поиск

Assign a URL to location

   <source lang="html4strict">
 
   

<html> <body> <script language="JavaScript">

   function function1() {
       location.assign("http://www.wbex.ru/");
   }

</script> <input type="button" value="Load new page" onclick="function1();"> </body> </html>



 </source>
   
  


Display properties in location

   <source lang="html4strict">
 

<html> <head>

   <title>Location</title>
   <script type = "text/javascript">
       function myFunction() {
           var body = document.getElementsByTagName("body")[0];
           for (var prop in location) {
               var elem = document.createElement("p");
               var text = document.createTextNode(prop + ": " + location[prop]);
               elem.appendChild(text);
               body.appendChild(elem);
           }
           if (location.search) {
               var querystring = location.search.substring(1);
               var splits = querystring.split("&");
               for (var i = 0; i < splits.length; i++) {
                   var splitpair = splits[i].split("=");
                   var elem = document.createElement("p");
                   var text = document.createTextNode(splitpair[0] + ": " + splitpair[1]);
                   elem.appendChild(text);
                   body.appendChild(elem);
                }
           }
       }
   </script>

</head> <body onload="myFunction()"> </body> </html>


 </source>
   
  


If location is a search, display its value

   <source lang="html4strict">
 

<html> <head>

   <title>Location</title>
   <script type = "text/javascript">
       function showProps() {
           var body = document.getElementsByTagName("body")[0];
           if (location.search) {
               var querystring = location.search;
               var splits = querystring.split("&");
               for (var i = 0; i < splits.length; i++) {
                   var splitpair = splits[i].split("=");
                   var elem = document.createElement("p");
                   var text = document.createTextNode(splitpair[0] + ": " + splitpair[1]);
                   elem.appendChild(text);
                   body.appendChild(elem);
                }
           }
       }         
   </script>

</head> <body onload="showProps()"> </body> </html>


 </source>
   
  


Invoking the location.replace() Method

   <source lang="html4strict">
  

<html> <head> <title>location.replace() Method</title> <script type="text/javascript"> function doReplace() {

location.replace("http://www.wbex.ru"); } </script>

</head> <body> <form name="myForm"> <input type="button" value="Replace Me" onclick="doReplace()" /> </form> </body> </html>


 </source>
   
  


Location reload

   <source lang="html4strict">
 
   

<html> <body>

Row 1
Row 2
Row 3

<button onclick="myTable.style.fontWeight = "bold";">Bold</button> <button onclick="location.reload();">Reload page</button> </body> </html>



 </source>
   
  


location.reload(true) and history.go(0)

   <source lang="html4strict">
  

<html> <head> <title>Reload Comparisons</title> <script type="text/javascript"> function hardReload() {

   location.reload(true); 

} function softReload() {

   history.go(0); 

} </script> </head> <body> <form name="myForm"> <input type="button" value="Soft Reload" onclick="softReload()" /> <input type="button" value="Hard Reload" onclick="hardReload()" /> </form> </body> </html>


 </source>
   
  


Location replace

   <source lang="html4strict">
 
   

<html> <body> <script language="JavaScript">

   function function1() {
       location.replace("http://www.wbex.ru");
   }

</script> <input type="button" value="Replace" onClick="function1();"> </body> </html>



 </source>