【问题标题】:Separating HTML and JS分离 HTML 和 JS
【发布时间】:2017-06-23 21:47:43
【问题描述】:

我正在尝试将嵌入到 HTML 文件中的一些 JS 代码分开。我不拥有此代码,它用于远程支持登录页面,但我不确定如何将它们分开。

我尝试将 JS 代码复制到不同的 .js 文件中,然后将脚本标签添加到链接中,但没有成功。

<script type="text/javascript" src="https://www.islonline.net/webapi/api.js?
libs=join"></script>
<div class="isl-connect-form">
<form id="isl-connect-form" action="#" method="get" onsubmit="return 
isl_connect();">
    <fieldset>
        <legend>Enter your session code and click Connect</legend>
        <div>
            <label for="isl-code-field">Session code</label>
            <input type="text" name="code" id="isl-code-field" value="" />
        </div>
        <input type="submit" name="submit" value="Connect" />
    </fieldset>
</form>
<div id="isl-feedback"></div>
</div>

<script type="text/javascript">
function isl_connect() {
    var doc = document,
        f = doc.getElementById('isl-connect-form'),
        r = doc.getElementById('isl-feedback'),
        is_msie = navigator.userAgent.indexOf('MSIE') >= 0,
        b = null;

    ISLOnline.Join.getSessionInfoByCode(
        f.code.value,
        function (info) {
            r.className = 'isl-success';
            r.innerHTML = 'Connecting to session ' + 
info.getAttribute('sessionCode');
            if (is_msie) {
                r.innerHTML += ', please click the button below:<br />';
                r.appendChild(doc.createElement('br'));
                var b = doc.createElement('input');
                b.type = 'button';
                b.name = 'join';
                b.value = 'Start';
                b.onclick = function () {
                    info.join();
                };
                r.appendChild(b);
            } else {
                info.join();
            }
        },
        function (error) {
            r.className = 'isl-error';
            r.innerHTML = 'Invalid session code!';
            /* comment the line above and uncomment the line below if you 
 wish to
                display the error that is sent by the server */
            //r.innerHTML += error.getDescription();
        }
    );
    return false;
}

【问题讨论】:

  • 这不起作用?
  • 现在你的问题是什么?
  • 我试过 Kyle 但没有,当我点击提交按钮时,没有任何反应
  • @JHiggins,某些代码要求将其包含在页面上。我知道一些跟踪代码和类似的东西就是这样。尝试使用脚本标签,但将其移动到顶部,在已经存在的脚本标签下。
  • @Kyle1323 这是错误的。跟踪代码是不可移植的,这意味着它不能跨多个域应用,因为它是针对特定域进行硬编码的,但您仍然可以将其移动到单独的文件中并通过src 属性包含它,只要您遵循有关在 HTML 结构中放置 &lt;script&gt; 标记的建议(例如,在 &lt;body&gt; 的末尾或在 &lt;head&gt; 中等)

标签: javascript html


【解决方案1】:

创建一个新的 JS 文件并将原始的完整 javascript 放入其中,然后在 islonline.net API 调用之后加载它。我已经举了一个例子。

<script type="text/javascript" src="https://www.islonline.net/webapi/api.js?libs=join"></script>
<div class="isl-connect-form">
  <form id="isl-connect-form">
    <fieldset>
      <legend>Enter your session code and click Connect</legend>
      <div>
        <label for="isl-code-field">Session code</label>
        <input type="text" name="code" id="isl-code-field" value="" />
      </div>
      <input type="submit" name="submit" value="Connect" />
    </fieldset>
  </form>
  <div id="isl-feedback"></div>
</div>
<!-- your new external JS file -->
<script type="text/javascript" src="https://www.example.com/path/to/your/file.js"></script>

您的新 Javascript 文件将包含原始 JS 代码,稍作修改以使用 addEventListener 而不是 onsubmit 来帮助区分 HTML 和 JavaScript:

document.getElementById('isl-connect-form').addEventListener('submit', function isl_connect(event) {
    if (typeof event.preventDefault == 'function') event.preventDefault();

    var doc = document,
        f = this,
        r = doc.getElementById('isl-feedback'),
        is_msie = navigator.userAgent.indexOf('MSIE') >= 0,
        b = null;

    ISLOnline.Join.getSessionInfoByCode(
        f.code.value,
        function (info) {
            r.className = 'isl-success';
            r.innerHTML = 'Connecting to session ' + 
info.getAttribute('sessionCode');
            if (is_msie) {
                r.innerHTML += ', please click the button below:<br />';
                r.appendChild(doc.createElement('br'));
                var b = doc.createElement('input');
                b.type = 'button';
                b.name = 'join';
                b.value = 'Start';
                b.onclick = function () {
                    info.join();
                };
                r.appendChild(b);
            } else {
                info.join();
            }
        },
        function (error) {
            r.className = 'isl-error';
            r.innerHTML = 'Invalid session code!';
            /* comment the line above and uncomment the line below if you wish to
             * display the error that is sent by the server
             */
            //r.innerHTML += error.getDescription();
        }
    );
    return false;
});

【讨论】:

  • @PatrickRoberts 为指出这一点而欢呼,我也许应该阅读代码:(
  • 因为这是关于“分离 HTML 和 JS”,您介意我编辑您的答案以使用 addEventListener() 而不是 onsubmit 属性吗?
  • @PatrickRoberts 一点也不,让 Answer 更好以供将来参考。
  • @PatrickRoberts 我希望您已经重新发布了答案并进行了更正。这真的是你现在的工作,而且做得更好。
  • 不,复制别人的答案只是为了对其进行编辑,这是一种糟糕的礼仪,这就是为什么我提出要对你的答案进行编辑。
猜你喜欢
  • 2021-03-29
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多