我看过这个链接。
Browser detection in JavaScript?
我的问题略有不同。此链接适用于普通网页。我想从 iframe 中检测浏览器,并希望根据浏览器显示不同的内容。
让我详细解释一下
我们已经使用 asp.net 和 vb.net 构建了一个应用程序。我们的客户可以将此应用程序作为 iframe 集成到他们的网站中。
此 iframe 中有 10-12 个页面。其中一个 iframe 页面的代码就像
<%@ Page Title="" Language="VB" MasterPageFile="~/cn/MasterPage.master" AutoEventWireup="false" CodeFile="login.aspx.vb" Inherits="pn_login" %>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="Server">
<h2>Register or log in to apply</h2>
<div class="login-wrapper" id="safari">
XXXXXXXXXXX
</div>
<div class="login-wrapper" id="other">
YYYYYYYY
</div>
<asp:Content ID="Content3" runat="server" ContentPlaceHolderID="ScriptContent">
<script type="text/javascript">
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
</script>
iframe 及其页面在除 safari 之外的所有浏览器上都能正常工作。所以我只需要检测浏览器是否为 safari。脚本代码确定浏览器是否为 safari。如果变量 isSafari 的值为 true,则用户使用 safari,否则值为 false。
到目前为止一切顺利。现在我们要根据变量显示不同的内容。即
if isSafari==True then
<div class="login-wrapper" id="safari"> xxxxx </div>
else
<div class="login-wrapper" id="other"> yyyy </div>
我该怎么做?
更多信息
我们的 Web 应用程序使用母版页并依赖于数据库。在母版页中,我们已经在 body onload 下使用了一个 javascript 函数。
iframe 母版页的代码是 .
<body onload="iframeResizePipe()">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div class="careers b-<%: CInt(Request("b"))%>">
<asp:ContentPlaceHolder ID="mainContent" runat="server" />
</div>
</form><br />
</body>
请注意
我不想只检查浏览器并告诉用户他们正在使用哪个浏览器。用户知道他们使用的是哪种浏览器。我想在后端检测浏览器。如果用户使用的是 safari,那么我想向他展示不同的内容。如果他们不使用 safari,则内容会有所不同
而且我也看到了这个链接 https://www.whatismybrowser.com/developers/tools/iframe .这不是我想要的
感谢您的建议。
谢谢
最佳答案
if (isSafari) {
document.getElementById("safari").style.display = 'block';
document.getElementById("other").style.display = 'none';
}
else {
document.getElementById("safari").style.display = 'none';
document.getElementById("other").style.display = 'block';
}
关于javascript - 使用 javascript 从 iframe 检测浏览器并根据浏览器显示不同的 <div> 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34066865/