dongzi3805 2012-06-21 16:37
浏览 30
已采纳

从页面网址获取参数

So, here's the main code, which checks the URL of the current page for the string ?a= then sets the variable ee_roomname to anything after that.

<script type="text/javascript">
    var ee_roomname = unescape(location.href.substring(location.href.lastIndexOf("?a=")+1))
    if(ee_roomname.indexOf("?") != -1) ee_roomname = "";
</script>

But what, if I want to add more variables, maybe after this value. For ex. here's a possible url:

http://abc.de/fg.html?a=hijk

Thus, the variable ee_roomname will be set to hijk
Now if we add some more tags, it will look like this:

http://abc.de/fg.html?a=hijk&b=lmno&c=pqrs

Now ee_roomname will be set to hijk&b=lmno&c=pqrs, which is not what I want.
I want the code to only track the a variable/parameter's value from the URL.

What needs to be changed, and how, in order to make it work?
Maybe provide a code I can use, too. (not too big fan of "do this and this" and such)

  • 写回答

3条回答 默认 最新

  • dongnaosuan5407 2012-06-21 16:45
    关注

    Here is a function to retrieve url parameters

    function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
        });
        return vars;
    }
    

    How to retrieve the url parameters in your javascript

    var first = getUrlVars()["id"];
    var second = getUrlVars()["page"];
    
    alert(first);
    alert(second);
    

    View the Source

    For your bonus: if you split the string at the '?' and take the first half, that will give you the "pure" absolute url.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题