我正在 GitHub Pages 上构建 Jekyll 站点。我有一个需要一点 JavaScript 的特定页面。此页面的标题和正文标签由我使用的主题控制。我想在一个外部脚本文件中维护这个 JavaScript,而不是在我的页面中间使用原始脚本。
鉴于现代网络技术,简单地使用异步脚本标签加载文件有什么主要问题吗?一旦页面生成,这个标签就会出现在正文内容中,但据我所知,在正文中不加载外部脚本的建议最初来自加载速度问题,并且对于 async 不再有效。该脚本很小并且控制非关键功能。
基本上:
<body>
...page content...
<script src="/assets/js/myscript.js" type="text/javascript" async=""></script>
...page content...
</body>
Run Code Online (Sandbox Code Playgroud)
我可以覆盖主题布局以直接访问标题并更传统地执行此操作,但如果可能,我想避免这种情况。在 Jekyll 中使用小型一次性脚本还有其他建议吗?
我有这个脚本,我使用的是纯 HTML 网站,并通过标签导入它<script>:
(function($) {
var wa_time_out, wa_time_in;
$(document).ready(function() {
$(".wa__btn_popup").on("click", function() {
if ($(".wa__popup_chat_box").hasClass("wa__active")) {
$(".wa__popup_chat_box").removeClass("wa__active");
$(".wa__btn_popup").removeClass("wa__active");
clearTimeout(wa_time_in);
if ($(".wa__popup_chat_box").hasClass("wa__lauch")) {
wa_time_out = setTimeout(function() {
$(".wa__popup_chat_box").removeClass("wa__pending");
$(".wa__popup_chat_box").removeClass("wa__lauch");
}, 400);
}
} else {
$(".wa__popup_chat_box").addClass("wa__pending");
$(".wa__popup_chat_box").addClass("wa__active");
$(".wa__btn_popup").addClass("wa__active");
clearTimeout(wa_time_out);
if (!$(".wa__popup_chat_box").hasClass("wa__lauch")) {
wa_time_in = setTimeout(function() {
$(".wa__popup_chat_box").addClass("wa__lauch");
}, 100);
}
}
});
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
var expires = "expires=" + d.toUTCString(); …Run Code Online (Sandbox Code Playgroud) 我对网络编程很新,读一本关于ASP.NET的书,我注意到作者这样做了;
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
Run Code Online (Sandbox Code Playgroud)
而不是Visual Studio所做的,这是什么;
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)
第一种方法是使用可接受的多浏览器兼容快捷方式吗?
我想在我的mediawiki安装中插入一个flattr图像.这需要<script>...</script>在html文件的标题中插入一个部分.我不知道在哪里可以插入这个,因为我<head>在文件中找不到-tag MonoBook.php.我使用当前的mediawiki软件(1.16.2)和MonoBook风格.有人能给我一个线索,我可以插入所需的JavaScript吗?
<script runat="server">和之间有什么区别<script>?
概述
我的 Koa 服务器中有一个自定义 REST API,我可以使用 React 在我的 Shopify 应用程序中很好地访问它。但是,当我尝试从脚本标签访问它时,出现 CORS 错误。
问题
我无法从脚本标签访问我的自定义 REST API 数据,但我可以从 Postman 和我的 React 应用程序访问它。
我尝试过的
代码
服务器.js
server
.use(router.routes())
.use(router.allowedMethods())
.use(CORS({ origin: "*" }));
router.get("/api/test", async (ctx, next) => {
ctx.body = {
llama: "123",
};
});
Run Code Online (Sandbox Code Playgroud)
script_tag.js
async function hello() {
await _loadScript(
"https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js",
);
const data = await window.axios.get(
"https://3b1b4258759f.ngrok.io/api/test?shop=panda-app-tests.myshopify.com",
{
crossDomain: true,
},
);
console.log(data);
}
Run Code Online (Sandbox Code Playgroud)
有什么想法可以在不使用cors-anywhere 的情况下解决这个问题吗?预先非常感谢您。
我当前的设置是用户单击链接以动态加载内容,其中还包括在脚本中加载.我希望能够测试是否加载了外部脚本(特别是Google Maps JS API),如果不加载,那么就这样做.
这是我的代码:
if(_href == "contact.html") {
// this will run everytime we navigate/click to go to "contact.html"
console.log("contact.html loaded");
// load the contact.js script, and once loaded,
// run the initMap function (located inside contact.js) to load the map
$.getScript("contact.js", function() {
// store the length of the script, if present, in a varialbe called "len"
var len = $('script[src="https://maps.googleapis.com/maps/api/js"]').length;
console.log(len);
// if there are no scripts that match len, then load it
if (len === 0) …Run Code Online (Sandbox Code Playgroud) 例如,我有以下 html:
<body>
<script src="code.js?q=xyz"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
我希望能够q=xyz从内部读取查询参数code.js。我试过:
console.log(window.location.href)
Run Code Online (Sandbox Code Playgroud)
但它给了我 html 文档的 URL,而不是code.js的 URL(然后我可以解析查询参数的 URL)。
有没有办法从该 JavaScript 中获取导入的 JavaScript 文件的查询参数?我知道我可以在服务器上读取查询参数,然后发回嵌入了查询参数的 JavaScript,但我不想要那样。我希望能够将 JavaScript 文件放在可以通过查询参数配置的 CDN 上。
javascript script-tag query-parameters location-href html5-history
<!-- <script type="text/javascript">/*<![CDATA[*/ c-- ;//]]></script> -->
Run Code Online (Sandbox Code Playgroud)
当我在普通html页面的<head>部分中有上述行时,Firefox 3.5.5将尾随 - >呈现为文本.如果我将c--改为c-则不然.有什么想法在这里发生了什么?由于一个非常大的脚本被碾压,我在我的页面上得到了一个工件.我可以将语句改为c- = 1并暂时避免问题但是....我想知道什么位/字节咬我的a $$.
我编写了一个微模板实用程序,它使用innerHTML 根据用户输入(纯文本字符串或html 字符串)在网页中注入html 片段。
我主要担心的是恶意脚本注入的风险。脚本可以通过脚本标签或内联事件(例如 img onload、div onmouseover)注入。
有没有办法清理 html 字符串以防止此类注入?另外,还有其他我应该注意的脚本注入方法吗?
是否可以在脚本标记中放置noscript标记?因为我想执行一个函数来检查JS是否启用,如果没有,它将重定向到另一个链接,如下所示:
<script>
window.setInterval(function(){
<noscript>
<meta http-equiv="refresh" content="0; url=http://192.168.1.1/index.html" />
</noscript>
}, 5000);
</script>
Run Code Online (Sandbox Code Playgroud)
谢谢
script-tag ×11
javascript ×6
asp.net ×2
html ×2
jquery ×2
reactjs ×2
.net ×1
asynchronous ×1
comments ×1
firefox ×1
github-pages ×1
google-maps ×1
innerhtml ×1
jekyll ×1
koa ×1
mediawiki ×1
next.js ×1
noscript ×1
rest ×1
runatserver ×1
shopify-app ×1
xss ×1