【问题标题】:Detect browser from an iframe with javascript and display different <div> content depending on the browser?从带有 javascript 的 iframe 中检测浏览器并根据浏览器显示不同的 <div> 内容?
【发布时间】:2015-12-03 13:05:37
【问题描述】:

我看过这个链接。

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。这不是我想要的

感谢您的建议。

谢谢

【问题讨论】:

标签: javascript asp.net vb.net iframe browser


【解决方案1】:
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';
}

【讨论】:

    【解决方案2】:

    检查浏览器的最简单方法是 features 标签。来自另一个网站的示例代码如下。 &lt;iframe src="https://www.whatismybrowser.com/feature/iframe?capabilities=true" width="600" height="270" style="border: none;"&gt;&lt;/iframe&gt;

    【讨论】:

    • 您没有正确理解我的问题。我不想只检查浏览器并告诉用户他们正在使用哪个浏览器。用户知道他们正在使用哪个浏览器。我想在后端检测浏览器。如果用户正在使用 safari,那么我想向他展示不同的内容。如果他们不使用 safari,内容会有所不同。
    • 微软提供了一个vb代码来检查浏览器msdn.microsoft.com/en-us/library/…
    • 这是您要问的问题还是请再描述一下。
    • 最新链接检测浏览器并告诉用户何时单击我不想要的按钮。如果用户使用 safari 则用户无需执行任何其他操作,那么 iframe 中的 html 内容(不是警告框)会告诉他们使用其他浏览器。
    • 按钮点击事件提供的代码可以在页面加载时调用。在页面加载中,我们可能会更改该页面的内容以显示在用户端。
    猜你喜欢
    • 2012-09-23
    • 1970-01-01
    • 2016-01-25
    • 2010-10-19
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2013-04-18
    相关资源
    最近更新 更多