简体   繁体   English

加载后向外部网页添加 HTML/JS 代码

[英]Add HTML/JS code to external web page after it has loaded

Is it possible to load an external web page like ' http://www.google.com ' and then append my own HTML/JS code to the end of it (eg to run a function)??是否可以加载像“ http://www.google.com ”这样的外部网页,然后将我自己的 HTML/JS 代码附加到它的末尾(例如运行一个函数)?? Surely there is a way to load an external page then add a little bit of my own code after it?当然有一种方法可以加载外部页面然后在它之后添加一点我自己的代码? Something like this:像这样的东西:

<html>

<script>

document.location.href="http://www.google.com/"; //Load external page

function myscript() {
...blahblah
}
</script>

<button onclick="myscript();">Click me</button>

</html>

I'd like that button to be at the bottom of the external page.我希望该按钮位于外部页面的底部。 Please do not suggest parsing methods in php.请不要在 php 中建议解析方法。 I've tried doing this by parsing the page first in php then appending my own script to it and echoing as I described here: Append HTML to page after redirect我已经尝试通过首先在 php 中解析页面然后将我自己的脚本附加到它并按照我在此处描述的进行回显来做到这一点: 重定向后将 HTML 附加到页面

This works for simple pages where there are no re-directs or when the final external page can be parsed properly.这适用于没有重定向或可以正确解析最终外部页面的简单页面。 The problem is that I can't properly parse the external page.问题是我无法正确解析外部页面。 The code that is parsed doesn't seem to function without the code from previous pages (before the re-directs).如果没有前几页的代码(重定向之前),解析的代码似乎无法运行。 I need to do this without parsing/scraping/crawling.我需要在不解析/抓取/爬行的情况下执行此操作。

Thanks!谢谢!


EDIT: I've tried displaying the external page in an iframe as suggested by Amadan:编辑:我已经尝试按照 Amadan 的建议在 iframe 中显示外部页面:

<script>

function myscript() {
...blahblah
}
</script>

<iframe src="http://www.google.com/"></iframe>
<button onclick="myscript();">Click me</button>

</html>

However, in firefox it just displays a blank box but in IE it says "This content cannot be displayed in a frame: To help protect the security of information you enter into this website, the publisher of this content does not allow it to be displayed in a frame."但是,在 Firefox 中它只显示一个空白框,但在 IE 中它说“此内容不能在框架中显示:为了帮助保护您输入到此网站的信息的安全性,此内容的发布者不允许显示该内容在一个框架内。”

Any way I can get around this?有什么办法可以解决这个问题吗?


EDIT 2:编辑2:

I've included jquery and the cross domain script here ( https://github.com/padolsey/jquery.fn/blob/master/cross-domain-ajax/jquery.xdomainajax.js ).我在这里包含了 jquery 和跨域脚本( https://github.com/padolsey/jquery.fn/blob/master/cross-domain-ajax/jquery.xdomainajax.js )。 This is the code I'm using now to get the contents using ajax.这是我现在使用 ajax 获取内容的代码。 How would I go from that to actually displaying the content in the webpage?我将如何从那个转到实际显示网页中的内容? Sorry I'm really bad with ajax/jquery!对不起,我对 ajax/jquery 真的很糟糕!

   function test () {
     $.ajax({
       url: 'http://www.google.com/',
       type: 'GET',
       success: function(res) {
         var content = $(res.responseText).text();
         alert(content);
       }
     });
   }

You can't append anything to a page you don't control except by installing a browser extension (which then works only for the clients where your extension installed).除非安装浏览器扩展程序(然后仅适用于安装了扩展程序的客户端),否则您无法将任何内容附加到您无法控制的页面。

You can include the contents of a page you don't control inside your own page (in two main ways: client-side iframe and server-side pull), but you seem to be saying this is not what you want.可以在自己的页面中包含您无法控制的页面内容(主要有两种方式:客户端iframe和服务器端拉取),但您似乎在说这不是您想要的。

Try something like this:尝试这样的事情:

  var html = '';

  $.ajax({
      uri: 'http://ya.ru',
      method: 'POST',
      success: function(data){
          htmlx = data;
      }
  });

About manipulating html inside an variable关于在变量中操作 html

var test = $("<div/>");
test.append(html);

test.find(".innertest");

// When I'm ready to append it..

$('#container').append(test);

If it doesn't work, you can use another page on your server for getting a remote page and use ajax to request it.如果它不起作用,您可以使用服务器上的另一个页面来获取远程页面并使用 ajax 来请求它。

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

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