从其他页面加载所有标记

时间:2022-10-12 18:18:20

I'm hooking into a separate page (same domain) and pulling it into the current page using $.load, which would be fine, if I was doing it for the whole page, but I'm not, I'm just doing it for the JavaScript code in that page. I was wondering if it's possible to load all the script tags from said page into the current page?

我正在挂钩到一个单独的页面(同一个域)并使用$ .load将其拉到当前页面,如果我在整个页面上这样做会没问题,但我不是,我只是在做它用于该页面中的JavaScript代码。我想知道是否可以将所有脚本标签从所述页面加载到当前页面?

I'm currently using the below code:

我目前正在使用以下代码:

var newMessageURL = $('#lnkCompose a').attr('href'); $('#hiddenScriptLoad').load(newMessageURL);

var newMessageURL = $('#lnkCompose a')。attr('href'); $( '#hiddenScriptLoad')负载(newMessageURL);

2 个解决方案

#1


0  

Is said page on the same domain or do you have access to it? You will run into trouble with the cross domain origin policy otherwise. If you do have access, the only way is to parse the html using a regex statement or html parser and pull the scripts manually. This sounds like a very hacky approach though and I'm not really sure why you'd want to do this.

是在同一个域上说的页面还是您可以访问它?否则,您将遇到跨域原始策略的问题。如果您有访问权限,唯一的方法是使用正则表达式语句或html解析器解析html并手动拉取脚本。这听起来像是一个非常黑客的方法,我不确定你为什么要这样做。

If you have access, get the page contents and then use the below to get the script tag sources.

如果您有权访问,请获取页面内容,然后使用以下内容获取脚本标记源。

text.match( /<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g )

Credit Javascript regex to get src url between script tag

感谢Javascript正则表达式在脚本标记之间获取src url

#2


0  

var newMessageURL = $('#lnkCompose a').attr('href');    
$('#hiddenScriptLoad').load(newMessageURL);

The above code will load all the contents you have in the second file and it will also import any javascript codes you have there. But the codes you'll have in second file will not work and wont get into action unless you call to them from first page using a function call or in any other manner.

上面的代码将加载您在第二个文件中的所有内容,它还将导入您在那里的任何JavaScript代码。但是你在第二个文件中拥有的代码将不起作用,除非你使用函数调用或以任何其他方式从第一页调用它们,否则不会采取行动。

If you just want to separate your js codings and html and have them in two separate files, it would be better to use PHP to import the second file into the first one and in this way, when the page is loaded in the client browser, it will render it as it was just a single file containing both contents. Ex..

如果您只想将js编码和html分开并将它们分成两个单独的文件,最好使用PHP将第二个文件导入第一个文件,这样,当在客户端浏览器中加载页面时,它将呈现它,因为它只是包含两个内容的单个文件。恩..

<?php
    include("script_file.js"); 
?>

And also if you want get only the js part of the second file use something like this

而且如果你想只得到第二个文件的js部分使用这样的东西

<?php
    $Vdata = file_get_contents('path/to/YOUR/FILE.php');
    preg_match_all("'<script(.*?)</script>'si", $Vdata, $match);

    foreach($match[1] as $val)
    {
         echo $val;
    }

?>

#1


0  

Is said page on the same domain or do you have access to it? You will run into trouble with the cross domain origin policy otherwise. If you do have access, the only way is to parse the html using a regex statement or html parser and pull the scripts manually. This sounds like a very hacky approach though and I'm not really sure why you'd want to do this.

是在同一个域上说的页面还是您可以访问它?否则,您将遇到跨域原始策略的问题。如果您有访问权限,唯一的方法是使用正则表达式语句或html解析器解析html并手动拉取脚本。这听起来像是一个非常黑客的方法,我不确定你为什么要这样做。

If you have access, get the page contents and then use the below to get the script tag sources.

如果您有权访问,请获取页面内容,然后使用以下内容获取脚本标记源。

text.match( /<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g )

Credit Javascript regex to get src url between script tag

感谢Javascript正则表达式在脚本标记之间获取src url

#2


0  

var newMessageURL = $('#lnkCompose a').attr('href');    
$('#hiddenScriptLoad').load(newMessageURL);

The above code will load all the contents you have in the second file and it will also import any javascript codes you have there. But the codes you'll have in second file will not work and wont get into action unless you call to them from first page using a function call or in any other manner.

上面的代码将加载您在第二个文件中的所有内容,它还将导入您在那里的任何JavaScript代码。但是你在第二个文件中拥有的代码将不起作用,除非你使用函数调用或以任何其他方式从第一页调用它们,否则不会采取行动。

If you just want to separate your js codings and html and have them in two separate files, it would be better to use PHP to import the second file into the first one and in this way, when the page is loaded in the client browser, it will render it as it was just a single file containing both contents. Ex..

如果您只想将js编码和html分开并将它们分成两个单独的文件,最好使用PHP将第二个文件导入第一个文件,这样,当在客户端浏览器中加载页面时,它将呈现它,因为它只是包含两个内容的单个文件。恩..

<?php
    include("script_file.js"); 
?>

And also if you want get only the js part of the second file use something like this

而且如果你想只得到第二个文件的js部分使用这样的东西

<?php
    $Vdata = file_get_contents('path/to/YOUR/FILE.php');
    preg_match_all("'<script(.*?)</script>'si", $Vdata, $match);

    foreach($match[1] as $val)
    {
         echo $val;
    }

?>