简体   繁体   English

如何在网址中注入javascript代码以在现有页面中插入iframe

[英]how to inject javascript code in url to insert iframe in existing page

I am able to get the following JS to create IFRAME and add it to the page. 我可以获取以下JS创建IFRAME并将其添加到页面中。

The issue is if I create JS method on the page and ivoke it on a button click it works. 问题是,如果我在页面上创建JS方法并在单击它的按钮上激活它,就可以使用它。 But when I try to inject the same JS into the page url via setting the location.href it does not work the right way , rather it replaces the existing page with a new iframe. 但是,当我尝试通过设置location.href将相同的JS注入页面url时,它的工作方式不正确,而是用新的iframe替换了现有页面。

Here is my code: 这是我的代码:

location.href = "javascript:ifrm = document.createElement('IFRAME');ifrm.style.width = 60+'px';ifrm.style.height = 40+'px';document.body.appendChild(ifrm);";

You have to wrap it in a function... 您必须将其包装在函数中...

location.href = "javascript:(function () {ifrm = document.createElement('IFRAME');ifrm.style.width = 60+'px';ifrm.style.height = 40+'px';document.body.appendChild(ifrm);})()";

...although you can simply replace the location.href change with the actual code: ...尽管您可以简单地用实际代码替换location.href更改:

ifrm = document.createElement('IFRAME');
ifrm.style.width = 60+'px';
ifrm.style.height = 40+'px';
document.body.appendChild(ifrm);

What Casey said... Or you could put a "void 0;" 凯西说了什么...或者您可以输入“ void 0;”。 at the end of the script... 在脚本末尾...

location.href = "javascript:ifrm = document.createElement('IFRAME');ifrm.style.width = 60+'px';ifrm.style.height = 40+'px';document.body.appendChild(ifrm); void 0;";

(I assume you're not actually doing this with location.href, but actually by typing/pasting into the url bar, or creating a bookmarklet... I used to get hit with this a lot when typing javascript into the url bar...) (我假设您实际上不是在使用location.href来完成此操作,而是实际上通过在网址栏中输入/粘贴或创建书签的方式来实现。。。我过去在网址栏中输入javascript时经常遇到这种情况。 ..)

Anyway, the key thing to note is that if you set the location (by any of those three methods) to a javascript url, and the last statement returns something, the document body is set to that object. 无论如何,要注意的关键是,如果将位置(通过这三种方法中的任何一种)设置为javascript url,并且最后一条语句返回了某些内容,则文档主体将设置为该对象。 In your code, the last line is document.body.appendChild(ifrm) and appendChild() returns the ifrm object. 在您的代码中,最后一行是document.body.appendChild(ifrm)appendChild()返回ifrm对象。 In my suggested answer, the last statement is a void, so the document body isn't replaced. 在我建议的答案中,最后一条语句为空,因此不会替换文档主体。 In Casey's suggestion, the function doesn't have a return statement, so it's a void function, and the document body is also not replaced. 在Casey的建议下,该函数没有return语句,因此它是一个void函数,并且文档主体也不会被替换。

To get an idea of what's happening, try this instead: 要了解发生了什么,请尝试以下方法:

location.href = "javascript:ifrm = document.createElement('IFRAME');ifrm.style.width = 60+'px';ifrm.style.height = 40+'px';document.body.appendChild(ifrm); 'Hello world';";

or for some variability in the outcome 或结果有些变化

location.href = "javascript:ifrm = document.createElement('IFRAME');ifrm.style.width = 60+'px';ifrm.style.height = 40+'px';document.body.appendChild(ifrm); confirm('Pick one');";

Assuming the string is saved in the variable mystring : 假设字符串保存在变量mystring

Method 1 : 方法1

eval( mystring.replace("javascript:", "") );

Method 2 (if you want to keep the "javascript:" ): 方法2 (如果要保留"javascript:" ):

function clickLink(link) {
    var cancelled = false;

    if (document.createEvent) {
        var event = document.createEvent("MouseEvents");
        event.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0,
            false, false, false, false,
            0, null);
        cancelled = !link.dispatchEvent(event);
    }
    else if (link.fireEvent) {
        cancelled = !link.fireEvent("onclick");
    }

    if (!cancelled) {
        window.location = link.href;
    }
}

var link = document.createElement("a");
link.src = mystring;

clickLink(link);

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

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