This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

1. Solve the problem of clicking an empty link to return to the top of the page

Sometimes some empty links are clicked will let the web page back to the top, the cause of these problems is generally added some special links, such as “#”, etc.;

window.onload = function() {
    var as = document.getElementsByTagName("a"),
         i = 0,
         l = as.length,
         h = "";
    while (i < l) {                        // Iterate over all a links
        h = as[i].getAttribute("href");
        if (h == "#"| |! h)as[i].href = "JavaScript:void(0)";  // Modify the a linki++; }};Copy the code
  • Filter # and empty links;
  • JavaScript:void(0)Void is an operator;

2. Solve the problem of garbled Chinese parameters in URL transmission

Different browsers handle encoding differently. For example, Chrome and Internet Explorer use UTF-8 encoding, while FireFox may use ISO-8859-1 encoding.

Generally, the passed parameters are encoded in URF-8 format by encodeURIComponent, which can be decoded again by the server to solve the problem of Chinese garbled characters.

window.onload = function() {
    var cencodeStr = encodeURIComponent("I am Qingjs");
    alert("Call encodeURICompoent to 'I am Qingjs' code:" + cencodeStr + "\n" + "Call decodeURIComponent to decode:" + decodeURIComponent(cencodeStr));
}
Copy the code
  • You can useencodeURICode, used on the server sidedecodeURIDecoding; You can also useencodeURIComponentThe URI component encodes the string, and the corresponding decoding function isdecodeURIComponent

3. Obtain the URL parameters of the address bar

JavaScript provides window.location.href to get the full information of the address bar URL

window.onload = function() {
    function getURLArgs(e) {
        var args = "Empty".// Get the parameters of the address bar URL
            _args = [];                               // Try adding parameters to the browser
        u = window.location.href,
        s = u.indexof("?"),
        i = 0,
        j = 0,
        o = null;

        if(s ! = -1) {
            args = u.substr(s+1).split("&");
        }
        e.innerHTML = "Parameters separated by commas:" + args
        l = args.length;
        for(; i < l; i++) {
            if (args[i]) {
                o = args[i].split("=");
                _args[o[0]] = o[1]; }}console.log(_args)
        return_args; }}Copy the code
  • Calculate “? “first. Position and then intercept from “?” Start all characters, excluding? ;

4. Mask function keys Shift, Alt, and Ctrl

The combination of the function keys of the browser with other keys can produce many special combination effects, such as copy and paste

window.onload = function() {
    var shieldingFunctionKeys = (function() {
        document.onkeydown = function(event) {
            event = event || _W.event;
            if (event.shiftKey || event.altKey || event.ctrlKey) {
                alert("No Shift, Alt, Ctrl!")}}; }) (); };Copy the code
  • The Shift key:event.shiftKey, Alt key:event.altKey, Ctrl key:event.ctrlKey