- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
一直以来,我们都在使用这个可靠的网站重定向 HTML/JavaScript 代码
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=https://www.nosuchwebsite.com">
<script type="text/javascript">
window.location.href = "https://www.nosuchwebsite.com"
</script>
<title>Page Redirection</title>
</head>
<body>
<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
If you are not redirected automatically, follow the <a href='https://www.nosuchwebsite.com'>nosuchwebsite</a>
</body>
</html>
现在,我们希望在重定向之前执行 Google Analytics(分析)跟踪代码。
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
我意识到,元“刷新”可能会早于 JavaScript 跟踪代码执行。我想知道是否有任何方法可以使 Google Analytics 跟踪代码在元“刷新”之前运行
最佳答案
如果脚本需要相当长的时间并且我们不确定需要多长时间,我们不想指定秒数。
所以我们的想法是从元数据中获取要重定向的 URL,然后重定向到它,
window.stop();
你会编写脚本吗
最后从元中获取 URL 并重定向到它。
window.location = document.querySelector( '[http-equiv="refresh"]' ).content.split( 'url=' )[1];
// STEP 1: Stop currect redirection
window.stop();
// STEP 2: Do your script stuff
// promise? async/await? Do wtf you want.
const msgEl = document.getElementById( 'messages' );
const addMsg = msg => msgEl.innerHTML += msg + '<br>';
setTimeout( () => addMsg( 'Let me at least do my nails.' ), 1000 );
setTimeout( () => addMsg( 'Aah, I\'m having a bad hair day :(' ), 2000 );
setTimeout( () => addMsg( 'Almost done...' ), 3000 );
setTimeout( () => addMsg( 'Ok, just a (three) second(s)...' ), 4000 );
setTimeout( () => {
addMsg( 'Redirecting...' );
// STEP 3: Redirect, Finally done with every we wanted to do.
window.location = document.querySelector( '[http-equiv="refresh"]' ).content.split( 'url=' )[1];
}, 7000 );
/* Arbitrary CSS for presentation purposes only */
p, h3, small {
font-family: sans-serif;
font-weight: 300;
line-height: 2
}
p {
font-size: 14px;
}
code {
font-family: monospace;
}
<!-- Resides somewhere inside <head> tag. -->
<meta http-equiv="refresh" content="0;url=https:///google.com/">
<!-- Arbitrary html for illustration -->
<h3>
Please wait while you are redirected with the mighty <code>meta refresh</code>.
</h3>
<p id='messages'></p>
<small><b>Spoiler:</b> Redirection will be stopped even though <code>meta[http-equiv="refresh"]</code> refreshes in 0 seconds.</small>
关于javascript - 是否可以在 Meta 刷新之前运行 JavaScript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17205850/
我最近发现了 python 中的元类。 基本上,python 中的元类是创建类的类。有很多有用的理由可以解释为什么要这样做——例如任何类型的类初始化。在工厂上注册类、复杂的属性验证、改变继承的工作方式
我已经从 Intellij 为 Spark+ scala 代码创建了 jar 并尝试在开发集群中运行该 jar,但最终出现以下错误: Exception in thread "main" java.l
我看到许多 Web 开发人员包含在标签中 我不明白这个元标记的目的是什么 最佳答案 它可能用于旨在增加 SEO 的 schema.org 微格式。 “Schema.org 提供了一组共享词汇,网
当我尝试使用 Java(在 GAE 服务器中)解析 XML 文件时,有时会遇到以下错误: Parse: org.xml.sax.SAXParseException; lineNumber: 10; c
我正在尝试解释 Web 应用程序的基础知识。我在 META-INF 和 WEB-INF 上遇到了这个问题。这些目录是如何获得这些名称的? 最佳答案 Jar 文件实际上是 ZIP 文件,带有额外的信息和
w3C验证器可以很好地使用以下代码: 如果我将property属性替换为name,则验证程序会说og:site_name未注册。 今天突然间,它显示了此错误: Error Line 7, Colum
安装vue-meta后,我的浏览器控制台出现错误。 为什么会出现这个错误?是我的代码还是bug?我正在使用 vue-meta@2.4.0和 Vue 3。 主文件 createApp(App) .u
我正在开发一个以通用模式运行 Nuxt 的网站。我们与另一位开发人员一起为此工作了几个月。我们使用 Git 在不同的分支上进行协作,然后在发布网站新版本时的某些时刻进行集成。 从昨天开始,我们遇到了一
根据这次更新... https://developers.facebook.com/blog/post/2013/06/19/platform-updates--new-open-graph-tags
如果元描述和元关键字相同,这对搜索引擎来说是否被认为是“坏的”? 最佳答案 元描述和元关键字标签并未被主要搜索引擎用作其排名算法的一部分,因此这个问题确实没有实际意义。 (该关键字通常被忽略,并被广泛
在 Django 中,很多东西都在 request.META 中,我当前的代码会检查 request.META 中的 HTTP_TOKEN 之类的东西,所以在发送请求时我需要将请求发送到该 url,以
在 Django 中,很多东西都在 request.META 中,我当前的代码会检查 request.META 中的 HTTP_TOKEN 之类的东西,所以在发送请求时我需要将请求发送到该 url,以
我正在构建一个 Wordpress 主题,我们将与 Yoast 一起构建所有的 SEO。 所以,我想知道如何在 index.php/category.php 和其他页面中从帖子中获取 Yoast Me
如何在资源管理器中隐藏这些文件?我尝试在 settings.json 中使用此代码: // Place your settings in this file to overwrite default
我有一个 Raspberry 4 型号 B,并试图从 Ubuntu 构建 Yocoto linux 以进行安装。我在关注 https://medium.com/@shantanoodesai/run-
我正在尝试将 XML 转换为 PDF 文档。使用 XSL 解析 XML 来生成用于 PDF 创建的 HTML。 HTML 不包含 结束标记,因此出现以下错误 Exception in thread
直接在 MongoDB 上运行以下文本搜索不会产生任何问题: db.getCollection('schools').find({ $text: { $search: 'som
一般来说,我是微观基准测试的新手。当我在我的项目中执行 JMH 任务时,我得到“错误:无法找到资源:/META-INF/BenchmarkList”,我相信是由 jmh-generator-annpr
有人可以帮我重新映射org-shiftmetaright | org-shiftmetaleft至[shift-select-meta]left-word | [shift-select-meta]r
我有一个 Android 应用程序,它使用 actionbarsherlock和库下的下一个 jar 文件夹: android-support-v4.jar gson-2.2.4.jar libGoo
我是一名优秀的程序员,十分优秀!