简体   繁体   English

jQuery 从另一个 html 页面获取变量值

[英]jQuery get variable value from another html page

I have got an html page with this content, called "page1.html":我有一个包含此内容的 html 页面,称为“page1.html”:

<div>
    <div id="content">
        content
    </div>
    <script type="text/javascript">
        var x=5;
        var y=2;
    </script>
    <div id="other">Some other content</div>
</div>

In another html page, I'd like to get the value of the variable "x" (5).在另一个 html 页面中,我想获取变量“x”(5) 的值。 This is the main page:这是主页:

<!DOCTYPE html>
<html>
<head lang="en">
    <script type="text/javascript" src="app.js"></script>
</head>
<body>
    <div id="new_content"></div>
</body>
</html>

And this is the app.js script:这是 app.js 脚本:

$(function(){
    $.get('page1.html', function(result){
        var obj = $(result).find('script');
        $(this).append($('#new_content').text(obj.html()));
    });
});

And the result is "var x=5; var y=2;".结果是“var x=5; var y=2;”。 Is there a way to get only the value 5 from the var x?有没有办法只从 var x 中获取值 5?

Thanks for the help.谢谢您的帮助。

EDIT: I forgot to write that it's only an example, the code into the script in page1.html is more complex than the one shown.编辑:我忘了写这只是一个例子,page1.html 脚本中的代码比显示的更复杂。 The object "obj" in app.js is an [object htmlscriptelement] so I'd like to know if a method/att exists that I can use to directly access the var x. app.js 中的对象“obj”是一个 [object htmlscriptelement],所以我想知道是否存在可用于直接访问 var x 的方法/att。

Or you can simply use localStorage in this way: 或者,您可以通过以下方式简单地使用localStorage

page1.html : page1.html

localStorage.setItem("x", "value"); //setter

page2.html : page2.html

var x = localStorage.getItem("x"); //getter

you could try to comment the vat y=... line before pasting the code like: 您可以在粘贴以下代码之前尝试注释va y = ...行:

$(function(){
    $.get('page1.html', function(result){
        var obj = $(result).find('script');
        $(this).append($('#new_content').text(obj.html().replace('var y','//var y')));
    });
});

This seems like a bit of a weird thing to try and do. 尝试去做似乎有点奇怪。

I'll do my best to answer but it would be good to get a bit more context around why you want to do this as it might be that there's a better, cleaner way to do it. 我会尽力回答,但是最好能找到更多有关为什么要这样做的背景信息,因为这样做可能会有更好,更清洁的方法。

My recommendation with the information that you've given would be to assign the values to a DOM object, jQuery is built to traverse the DOM and manipulate it, it's really not a good idea to try and parse raw javascript to extract variable values. 我对您提供的信息的建议是将值分配给DOM对象,jQuery的构建目的是遍历DOM并对其进行操作,尝试解析原始javascript来提取变量值并不是一个好主意。

So, page 1 would look like this: 因此,第1页如下所示:

<div>
    <div id="content">
        content
    </div>
    <div class="myData" data-x="5" data-y="2" />
    <div id="other">Some other content</div>
</div>

Then my jQuery would look something like this: 然后我的jQuery将如下所示:

$(function(){
    $.get('page1.html', function(result){
        var obj = $(result).find('script');
        var page1X = $(result).find('div.myData').data('x');
        var page1Y = $(result).find('div.myData').data('y');
    });
});

This is just pseudo code, but hopefully you get the idea. 这只是伪代码,但希望您能理解。

Reference links: 参考链接:

To get value from another html page for example page1从另一个 html 页面获取值,例如 page1

<input type="text" id="pageoneinputid" value="pageone">

on page two in the script tag在脚本标签的第二页

var a=$("#pageoneinputid").val()
document.getElementById("pagetwoinputid").innerHtml=a;

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

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