JavaScript DHTML/Javascript Objects/location

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

Assign a URL to location

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



Display properties in location

  

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



If location is a search, display its value

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



Invoking the location.replace() Method

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



Location reload

  
    
<html>
<body>
<table id="myTable">
    <tr>
        <td>Row 1</td>
    </tr>
    <tr>
        <td>Row 2</td>
    </tr>
    <tr><td>Row 3</td>   
    </tr>
</table>
<button onclick="myTable.style.fontWeight = "bold";">Bold</button>
<button onclick="location.reload();">Reload page</button>
</body>
</html>



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

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



Location replace

  
    
<html>
<body>
<script language="JavaScript">
    function function1() {
        location.replace("http://www.wbex.ru");
    }
</script>
<input type="button" value="Replace" onClick="function1();">
</body>
</html>