简体   繁体   English

绕过javascript中window.open()的弹出窗口阻止程序

[英]Bypassing The pop-up blocker for window.open() in javascript

I have a text file with some lines containing some key words. 我有一个文本文件,其中某些行包含一些关键字。 My exercise is to extract info from that file and create a html document inserting the liens in the text file with appropriate tags. 我的工作是从该文件中提取信息,并创建一个HTML文档,在其中将留置权插入带有适当标记的文本文件中。 for example: This is the text file: 例如:这是文本文件:

This should be in a html tag with START as class name
THIS SHOULD BE IN A HTML TAG WITH CAPITALS AS CLASS NAME
This should be in a HTML tag with CODE as class name

Now writing a javascript program to insert those lines into HTML is very easy. 现在编写一个javascript程序以将这些行插入HTML非常简单。 I just used some string handling like this: 我只是使用了一些像这样的字符串处理:

if(contents[i].indexOf(" CODE")!=-1){
                            w.document.write("<p class='code'>"+lines+"</p>");

                        }

I am writing all these into a new window.open object. 我将所有这些都写到一个新的window.open对象中。 The main problem is that pop-up blockers do not allow this functionality. 主要问题是弹出窗口阻止程序不允许此功能。 So, is there any other way I can do this? 那么,还有其他方法可以做到这一点吗? I can't generate the html file physically, I need to generate it on the fly, so window.open is the only method I could think of. 我无法物理生成html文件,我需要即时生成它,因此window.open是我能想到的唯一方法。 Are there any other ways I can accompolish this? 我还有其他方法可以做到这一点吗? (I can bypass the pop up blocker by just using (我可以仅使用跳过弹出窗口阻止程序

w=window.open("somefile.html")
w.document.opne("somefile.html")

where somefile.html is any file. 其中somefile.html是任何文件。 But I do not want to do it, it hardly seems a clean way.) 但我不想这样做,这似乎很难做到。

More over, For me to access the file, I have to host it on a server (I am currently using http-server offered by node for this) Is there any other alternative to this? 而且,对于我来说,要访问该文件,我必须将其托管在服务器上(我目前正在使用节点提供的http服务器),还有其他替代方法吗?

I do not want to use Jquery, I wish to accomplish all of this with vanilla javascript. 我不想使用Jquery,我希望使用香草javascript完成所有这些工作。 But if there is a possibility of doing this with Jquery, please let me know. 但是,如果有可能使用Jquery进行此操作,请告诉我。

Thank you very much :) 非常感谢你 :)

On principle, pop-up windows are blocked by all modern browsers. 原则上,所有现代浏览器均会阻止弹出窗口。 They'll ask you if you want to allow them, but otherwise they'll not allow them. 他们会问您是否要允许他们,否则他们将不允许您。

If it has to be another document, perhaps an iframe could be useful? 如果必须是另一个文档,也许使用iframe可能有用吗?


Here's a bunch of extra solutions: 这里有很多额外的解决方案:

  • Have a fake pop-up. 有一个假的弹出窗口。 Just a div with all the data which floats above your regular content. 只是一个div,其中包含所有浮于常规内容之上的数据。 You can add an 'x' button to it for closing it and implement some drag-and-drop functionality. 您可以在其中添加一个“ x”按钮以将其关闭,并实现某些拖放功能。 The visual effect is much the same, but the user can't treat it as an OS-level window. 视觉效果几乎相同,但是用户不能将其视为操作系统级别的窗口。
  • Try to somehow integrate the content into the regular page. 尝试以某种方式将内容集成到常规页面中。 Either an iframe or just regular content. iframe或常规内容。 Modern design has passed the stage of needing pop-ups and other content outside of a single plane. 现代设计已经超过了需要在单个平面之外弹出窗口和其他内容的阶段。 Furthermore, on mobile it's unclear how pop-ups would work. 此外,尚不清楚在移动设备上弹出窗口如何工作。
  • Open the content in a new tab. 在新选项卡中打开内容。 This is basically just using an <a> tag. 基本上,这只是使用<a>标记。 You can put the content you wish to have in the fragment for the link, and the page you open can decode the fragment and show the info you want. 您可以将所需的内容放入链接的片段中,打开的页面可以解码该片段并显示所需的信息。 Might not work with huge content. 可能无法处理大量内容。
  • Have a better flow for allowing pop-ups. 允许弹出窗口的流程更好。 Inform the user that your site needs pop-ups and make them disable it. 通知用户您的站点需要弹出窗口并使其禁用。 One good way is to provide some button which triggers a pop-up, which will be blocked. 一种好方法是提供一些按钮,该按钮触发弹出窗口,该弹出窗口将被阻止。 Then inform them to tell the browser to allow the pop-up since most of them will show something about how the pop-up was blocked. 然后通知他们告诉浏览器允许弹出窗口,因为其中大多数将显示有关如何阻止弹出窗口的信息。 Then you can show your regular pop-up without the risk of the user missing out on the data. 然后,您可以显示常规弹出窗口,而不会导致用户错过数据的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM