Can URL tell jQuery to run a function?

后端 未结 4 1542
滥情空心
滥情空心 2020-12-19 11:28

Have a question regarding URL and jQuery.

Can I specify URL to tell jQuery to run a function?

e.g http://www.website.com/about.html?XYZ

to run a func

4条回答
  •  眼角桃花
    2020-12-19 12:02

    You can call a globally declared function using the window object:

    function bar(str) {
        alert("hello" + str);   
    }
    
    // assuming location is "http://example.com?bar"
    var fn = window.location.search.replace("?", "");
    window[fn](" Dovhakiin"); // 'hello Dovhakiin'
    

提交回复
热议问题