什么是最简单的< script>用参数标记 [英] What's the simplest <script> tag with params

查看:107
本文介绍了什么是最简单的< script>用参数标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在提供参数的同时包括一个脚本标签.这是我到目前为止想出的结果

I want to include a script tag, while providing a parameter to it. This is what I came up with so far

  1. 为脚本URL提供参数(cons:生成多个JS文件)

  1. Provide a parameter to script URL (cons: generate multiple JS files)

<script src="http://example.com/something.js?P=123" type="text/javascript"></script>

  • 隐藏脚本标记中的参数(与#1相同)

  • Hide parameter in script tag (cons: same as #1)

    <script src="http://example.com/scripts/123/something.js" type="text/javascript"></script>
    

  • Google Analytics(分析)方式(缺点:丑陋,复杂的全局变量)

  • Google Analytics way (cons: ugly, complicated, global variables)

    <script type="text/javascript" charset="utf-8">
      var _something = _something || 123;
      (function() {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = 'http://example.com/something.js';
        var ss = document.getElementsByTagName('script')[0];
        ss.parentNode.insertBefore(s, ss);
      })();
    </script>
    

  • 推荐答案

    如果脚本的执行方式取决于调用方式,则可以添加选项1之类的参数.

    If the way the script is executed, depends on how it's called, you can add params like your option 1.

    其他方式是:

    <script params='{"abc": 123}' src="script.js"></script><!-- params is a non standard, non official attr that the script will read -->
    

    <script>var _abc = 123;</script>
    <script src="script.js"></script>
    

    甚至

    <script src="script.js#abc=123"></script>
    

    尽管如此,我必须同意@outis的观点:始终为每个人加载相同的东西,然后像您/客户想要的那样执行它.

    I have to agree with @outis though: load the same thing for everybody, always, and execute it like you/the client want(s) afterwards.

    这篇关于什么是最简单的&lt; script&gt;用参数标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆