admin管理员组文章数量:1277314
I have got an html page with this content, called "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). 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:
$(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;". Is there a way to get only the value 5 from the var x?
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 plex than the one shown. 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.
I have got an html page with this content, called "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). 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:
$(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;". Is there a way to get only the value 5 from the var x?
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 plex than the one shown. 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.
Share Improve this question edited May 9, 2014 at 10:40 user3505947 asked May 9, 2014 at 10:20 user3505947user3505947 131 gold badge1 silver badge6 bronze badges 04 Answers
Reset to default 5Or you can simply use localStorage in this way:
page1.html:
localStorage.setItem("x", "value"); //setter
page2.html:
var x = localStorage.getItem("x"); //getter
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 remendation 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.
So, page 1 would look like this:
<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:
$(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:
- http://api.jquery./data/
you could try to ment the vat y=... line before pasting the code like:
$(function(){
$.get('page1.html', function(result){
var obj = $(result).find('script');
$(this).append($('#new_content').text(obj.html().replace('var y','//var y')));
});
});
To get value from another html page for example page1
<input type="text" id="pageoneinputid" value="pageone">
on page two in the script tag
var a=$("#pageoneinputid").val()
document.getElementById("pagetwoinputid").innerHtml=a;
本文标签: javascriptjQuery get variable value from another html pageStack Overflow
版权声明:本文标题:javascript - jQuery get variable value from another html page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741261140a2367643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论