如何在Chrome App中以编程方式加载JavaScript?(How to load javascript programmatically in Chrome App?)

编程入门 行业动态 更新时间:2024-10-28 00:24:47
如何在Chrome App中以编程方式加载JavaScript?(How to load javascript programmatically in Chrome App?)

我正在构建一个将作为自助服务终端运行的Chrome应用程序。 我遇到的问题是Chrome应用程序喜欢“一个”页面,因此加载不同的内容并不像只链接到另一个页面那么简单。 我所做的是使用jQuery .load()加载我的页面。 我现在遇到的一个问题是,这些页面中的每一个都有一些使用JPlayer插件的HTML5视频。 在这些页面上,我有一些内联js可以使视频播放器工作。 但是,内联JS在Chrome应用程序中是禁忌(UGH!)。 当我点击某些东西时,有没有办法可以动态地将javascript加载到我的应用程序中? Rgiht现在我有这样的东西..

$(".video_list a").click(function(){ $(".screen").load("video.html", function(){ $.getScript("video.js"); }); });

video.js将包含JPlayer设置以创建视频播放器。 当我将其作为浏览器中的常规网页运行时,它可以正常工作,但是当它的Chrome应用程序无法正常运行时我没有错误,只是视频应该是空的div。

I am building a chrome app that will run as a kiosk. The problem I've ran into is that Chrome apps like to be "one" page so loading different content is not as simple as just linking to another page. What I've done is loaded my pages with jQuery .load(). The probem I'm running into now is that each of these pages has some HTML5 video that uses the JPlayer plugin. On these pages I had some inline js that would make the video player work. However, inline JS is a no-no in a Chrome app (UGH!). Is there a way I can dynamically load javascript into my app when I click something? Rgiht now I have something like this..

$(".video_list a").click(function(){ $(".screen").load("video.html", function(){ $.getScript("video.js"); }); });

video.js would contain the JPlayer settings to create the video player. When I run this as just a regular webpage in the browser it works, but when its a Chrome App its doesn't work and I get no errors, just an empty div where the video should be.

最满意答案

你不能这样做,因为它再次是内联代码 - getScript将脚本加载为文本,然后eval它。

要解决此问题,您可以将脚本作为<script src="...">标记插入:

function loadScript(name) { var s = document.createElement('script'); s.src = chrome.runtime.getURL(name); s.onload = function() { this.parentNode.removeChild(this); }; (document.head||document.documentElement).appendChild(s); }

You can't do it like this, because it's inline code again - getScript loads the script as text, then evals it.

To work around it, you can insert the script as a <script src="..."> tag:

function loadScript(name) { var s = document.createElement('script'); s.src = chrome.runtime.getURL(name); s.onload = function() { this.parentNode.removeChild(this); }; (document.head||document.documentElement).appendChild(s); }

更多推荐

本文发布于:2023-08-01 11:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357774.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   加载   方式   如何在   App

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!