如果在地址栏中检测到文本,则隐藏对象

时间:2017-08-31 21:10:50

标签: javascript html ajax

如果在地址栏中检测到文本,我试图隐藏对象

我的网址看起来像这个sample.com/view=entry&id=8&lid=179(当检测到& id = 8时,如果id = 19显示某些内容,它应该隐藏其他

找到可能有用但需要调整的东西

<script language="javascript" type="text/javascript">

 var ZnodeSitename = 'http://example.com'; 
 if (window.location.search.substring(1) == "id=8") {
     var objFrame = document.getElementById("frame1");

 }
 else if (window.location.search.substring(1) == "id-9") {
     var objFrame = document.getElementById("frame1");
 }
 else {
     var objFrame = document.getElementById("id-9");
     var objFrame = document.getElementById("frame1");

 }

1 个答案:

答案 0 :(得分:0)

如果您想使用纯JavaScript,请尝试使用以下代码

<强> JavaScript的:

<script language="javascript" type="text/javascript">

    var urlParams = new URLSearchParams(window.location.search);

    var frame = document.getElementById("frame1");

    // Assuming "?id=8"

    if (getUrlParameter('id') == '8') {
        frame.style.display = 'none';
    } else {
        frame.style.display = 'block';
    }

    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

    // getUrlParameter('post'); // "1234"

</script>

<强> HTML:

<div id="frame1">
    <p>Will This Be Hidden</p>
</div>