2012-07-19 68 views
1

如果我有一个页面上的一些隐藏的元素,包含一些内容的网页,我怎么可以创建一个链接,当用户点击它,浏览器将打开一个新的窗口,展示的内容(只内容,例如一些json数据)?部分上了一个新的窗口

ps。我知道在页面上隐藏一些内容可能是个不好的主意。这是更好地把一个动作链接,将获得来自服务器的内容。但它涉及到很多其他的头痛,这是不是我是谁创造的页面,所以请让我知道,如果有一个相对简单的解决方案...

+0

“真正的” 新的浏览器窗口?还是只有一个模式窗口,像[jQuery的对话框(http://jqueryui.com/docs/dialog/)? – Zeta 2012-07-19 21:41:42

+0

我不知道。基本上我想在新标签页/窗口中显示json数据。或者...也许有一种方法可以将json数据发送到某个在线json查看器,以便用户以更易读的形式查看json数据? – Agzam 2012-07-19 21:49:54

回答

0

打开第二页时,你可以通过(URL编码)隐藏元素的含量作为参数的URL。然后,这个参数可以(未编码)插入第二页的正文中。

以下示例适用于本地OS X.在其他操作系统上,示例可能需要放置一个实际的Web服务器之前,它会工作:

page1.html

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Page 1</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script> 
function openwindow(){ 
    window.open("page2.html?html="+escape($("#myDiv").html())); 
} 
</script> 
<style> 
.hidden { 
    visibility: hidden; 
} 
</style> 
</head> 
<body> 

<a href="javascript:openwindow();">Click Me!</a> 

<div class="hidden" id="myDiv"> 
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/HTML5-logo.svg/200px-HTML5-logo.svg.png"> 
<p>See the HTML5 <a href="http://www.w3.org/TR/2012/WD-html5-20120329/">specification</a></p> 
</div> 

</body> 
</html> 

page2.html

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Page 2</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script> 
jQuery.extend({ 
    // from: http://paulgueller.com/2011/04/26/parse-the-querystring-with-jquery/ 

    parseQuerystring: function(){ 
     var nvpair = {}; 
     var qs = window.location.search.replace('?', ''); 
     var pairs = qs.split('&'); 
     $.each(pairs, function(i, v){ 
      var pair = v.split('='); 
      nvpair[pair[0]] = pair[1]; 
     }); 
     return nvpair; 
    } 
}); 
</script> 
<script> 
    $(document).ready(function(){ 
     $(document.body).html(unescape(jQuery.parseQuerystring().html)); 
    }); 
</script> 
<style> 
.hidden { 
    visibility: hidden; 
} 
</style> 
</head> 
<body> 

<!-- this will be replaced --> 

</body> 
</html>