This article mainly introduces related information about URL operations in JavaScript. For more information, see "Restore content ---
1. location. href .....
(1) self. loction. href = "http://www.cnblogs.com/url"
Window. location. href = "http://www.cnblogs.com/url" both use the same as opening the URL page on the current page
(2) this. location. href = "http://www.cnblogs.com/url" Current page opens URL
(3) parent. location. href = "http://www.cnblogs.com/url" opens a new page on the parent page. If the page has a custom frame, you can replace parent self top with the name of the custom frame to open the url address in the frame window.
(4) top. location. href = "http://www.cnblogs.com/url" opens a new page on the top page
2. Refresh the page
(1) window. location. href = http://www.cnblogs.com/nana-share/p/window.location.href
(2) window. location. Reload ()
Refresh the current page. The difference is whether data is submitted. When data is submitted, window. location. Reload () will prompt whether to submit the data. window. location. href = http://www.cnblogs.com/nana-#/p/?location.href=is directed to the specified URL.
3.
(1) The first section is actually used
Function getURLParameter (name) {2 3 return decodeURIComponent (new RegExp ('[? | &] '+ Name +' = '+' ([^ &;] + ?) (& | # |; | $ Your 'cmd.exe c (location. search) | [, ""]) [1]. replace (/\ +/g, '% 20') | null; // construct a regular expression object containing the target parameter 4 5} // obtain the parameter 2 function getUrlParam (name) {3 var reg = new RegExp ("(^ | &) in the url &) "+ name +" = ([^ &] *) (& | $) "); // constructs a regular expression object containing the target parameter 4 var r = window. location. search. substr (1 ). match (reg); // match the target parameter 5 if (r! = Null) return unescape (r [2]); return null; // return parameter value 6}
For example, retrieve the email address of the following link
Http: // agent/index. php/Home/Login/getpwd_check_email? Code = 824790 & to = 1321136493@qq.com
Var mail = getURLParameter ('to ');
--- Restore content end ---
Next let's take a look at the js url code.
The code is very simple. The main idea is to parse the url parameter into a js object and then add, delete, modify, and query operations ~, Take notes here.
Var LG = (function (lg) {var objURL = function (url) {this. ourl = url | window. location. href; this. href = "";//? Previous section this. params = {}; // url parameter object this. jing = ""; // # And this. init ();} // analyze the url? Save this. href. The parameter is resolved to this. params object, # And save it to this. jing objURL. prototype. init = function () {var str = this. ourl; var index = str. indexOf ("#"); if (index> 0) {this. jing = str. substr (index); str = str. substring (0, index);} index = str. indexOf ("? "); If (index> 0) {this. href = str. substring (0, index); str = str. substr (index + 1); var parts = str. split ("&"); for (var I = 0; I
0) {strurl + = this. jing;} return strurl;} // obtain the parameter value objURL. prototype. get = function (key) {return this. params [key];} lg. URL = objURL; return lg ;}( LG || {}));
LG is just the namespace of my personal common JS. Call:
Var myurl = new LG. URL ("http://www.baidu.com? A = 1 "); myurl. set ("B", "hello"); // added B = hello alert (myurl. url (); myurl. remove ("B"); // Delete B alert (myurl. get ("a"); // obtain the value of parameter a. Here, 1 myurl is obtained. set ("a", 23); // modify the value of a to 23 alert (myurl. url ());