使用 JavaScript 更新位置 (src) 中的参数

发布于 2024-12-01 10:35:13 字数 1083 浏览 1 评论 0原文

如何将 status = 'logged-out' 的值解析为其下面的 3 个标签,从而更新 login_status = 'logged-out' 的值?

<script type="text/javascript">
      window.ndm = window.ndm || {};
      window.ndm.cam = {'status':'logged-out'};
</script>

<script src="http://foo.com/adserver/ndm/js.php?position=header-ad&amp;section_id=NEWS&amp;login_status=SUBSCRIBER"></script>
<script src="http://foo.com/adserver/ndm/js.php?position=middle-ad&amp;section_id=NEWS&amp;login_status=SUBSCRIBER"></script>
<script src="http://foo.com/adserver/ndm/js.php?position=footer-ad&amp;section_id=NEWS&amp;login_status=SUBSCRIBER"></script>

请记住,页面上还有大量其他脚本标记,因此要识别相关的标记。我得到了这个功能。

function getScriptSourceName(name){
var scripts = document.getElementsByTagName('script');
for (i=0;i<scripts.length;i++){
    if (scripts[i].src.indexOf(name) > -1)
        return scripts[i].src;
}}

因此,为了找到我想要的相关脚本标签,我调用该函数 - getScriptSourceName('foo.com');

然后,我如何更新 login_status 参数的值以使用最顶部声明​​的值?

How can I parse the value of status = 'logged-out' to the 3 tags below it, updating the value of login_status = 'logged-out'?

<script type="text/javascript">
      window.ndm = window.ndm || {};
      window.ndm.cam = {'status':'logged-out'};
</script>

<script src="http://foo.com/adserver/ndm/js.php?position=header-ad&section_id=NEWS&login_status=SUBSCRIBER"></script>
<script src="http://foo.com/adserver/ndm/js.php?position=middle-ad&section_id=NEWS&login_status=SUBSCRIBER"></script>
<script src="http://foo.com/adserver/ndm/js.php?position=footer-ad&section_id=NEWS&login_status=SUBSCRIBER"></script>

Keep in mind, there also heaps of other script tags on the page, so to identify the relevant ones. I got this function.

function getScriptSourceName(name){
var scripts = document.getElementsByTagName('script');
for (i=0;i<scripts.length;i++){
    if (scripts[i].src.indexOf(name) > -1)
        return scripts[i].src;
}}

Therefore to find the relevant script tags I want, i call the function - getScriptSourceName('foo.com');

How can I then update the login_status parameter's value to use the one declare at the very top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-12-08 10:35:13

我认为这应该可行(在用于测试的 HTML 文件下方)。
查看 changeStatus 方法(我通过单击按钮触发它进行测试)。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <script type="text/javascript">
      window.ndm = window.ndm || {};
      window.ndm.cam = {'status':'logged-out'};
    </script>

    <script src="http://foo.com/some.php?login_status=SUBSCRIBER"></script>
    <script src="http://foo.com/some.php?login_status=SUBSCRIBER"></script>
    <script src="http://foofoo01.com/some.php?login_status=SUBSCRIBER"></script>

    <script>
        function changeStatus(name)
        {
            var scripts = document.getElementsByTagName('script');
            var scriptsToChange = [];
            for (var i = 0; i < scripts.length; i++)
            {
                if (scripts[i].src.indexOf(name) > -1)
                {
                    var oldSrc = scripts[i].src;
                    var newSrc = oldSrc.replace(/(login_status=).*/,'$1' + 'logged-out');
                    scripts[i].setAttribute("src", newSrc);     
                    scriptsToChange.push(scripts[i]);               
                }
            }

            for (var k = 0; k < scriptsToChange.length; k++)
            {
                document.getElementsByTagName("head")[0].appendChild(scriptsToChange[k]);
            }
        }
    </script>
</head>
<body>      
    <button type="button" onclick="changeStatus('foo.com')">Change status</button>
</body>
</html>

I think this should work (below the HTML file for testing).
Look at changeStatus method (I triggered it by button click for testing).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <script type="text/javascript">
      window.ndm = window.ndm || {};
      window.ndm.cam = {'status':'logged-out'};
    </script>

    <script src="http://foo.com/some.php?login_status=SUBSCRIBER"></script>
    <script src="http://foo.com/some.php?login_status=SUBSCRIBER"></script>
    <script src="http://foofoo01.com/some.php?login_status=SUBSCRIBER"></script>

    <script>
        function changeStatus(name)
        {
            var scripts = document.getElementsByTagName('script');
            var scriptsToChange = [];
            for (var i = 0; i < scripts.length; i++)
            {
                if (scripts[i].src.indexOf(name) > -1)
                {
                    var oldSrc = scripts[i].src;
                    var newSrc = oldSrc.replace(/(login_status=).*/,'$1' + 'logged-out');
                    scripts[i].setAttribute("src", newSrc);     
                    scriptsToChange.push(scripts[i]);               
                }
            }

            for (var k = 0; k < scriptsToChange.length; k++)
            {
                document.getElementsByTagName("head")[0].appendChild(scriptsToChange[k]);
            }
        }
    </script>
</head>
<body>      
    <button type="button" onclick="changeStatus('foo.com')">Change status</button>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文