开发者

Need to execute a JavaScript Float on if calling page with a variable

I need to execute a JavaScript float if a page is called with a variable like http://DOMAIN/whatever.html?float=1

I've tried a couple of different things but it still doesn't work.

Here is my latest attempt. It doesn't error but it doesn't work either.

<div id="FLOATINGDIVNAME"  style='display: none;'>CONTENT HERE</div>

Here is the script

<script type='text/javascript'>
var url = window.location.href;
if(url.indexOf('?float') != -1) {
    document.getElementById('FLOATINGDIVNAME').style.display = 'block';
}
</script>

Anyone have some c开发者_StackOverflow中文版ode that can work?


You can examine the ?xxx part of the URL with .search

function check() {
    //remove leading ?, make case insensitive, delimit on &
    var args = window.location.search.substring(1).toLowerCase().split("&");
    for (var k in args) {
        if (args[k] === "float=1") {
            document.getElementById('FLOATINGDIVNAME').style.display = 'block';
        }
    }
}

As this tries to modify the document, you must wait for the page to load before executing it;

<body onload="check();">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜