Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用javascript制作新闻小部件?_Javascript_Html - Fatal编程技术网

如何用javascript制作新闻小部件?

如何用javascript制作新闻小部件?,javascript,html,Javascript,Html,我有一个新闻网站,我的一些合作伙伴希望包括最新的新闻。他们目前正在用iframe做这件事,但他们要求提供javascript版本,以便他们可以设计div的样式,请帮助我如何用javascript做这件事 我现在的剧本是这样的 <iframe height="30%" width="50%" scrolling="no" frameborder="0" src="http://mysite.com/latestnews.php"></iframe> 当你说“他们要求的是

我有一个新闻网站,我的一些合作伙伴希望包括最新的新闻。他们目前正在用iframe做这件事,但他们要求提供javascript版本,以便他们可以设计div的样式,请帮助我如何用javascript做这件事

我现在的剧本是这样的

<iframe height="30%" width="50%" scrolling="no" frameborder="0" src="http://mysite.com/latestnews.php"></iframe>

当你说“他们要求的是JavaScript版本”时,可能会被解释为只提供数据,让客户添加他们喜欢的html标记和样式。如果这就是您要问的,那么您可以在站点上设置一个页面来处理JSONP请求

JSONP基本上返回一个JSON字符串,但封装在JavaScript函数调用中,并在请求中指定函数名。假设您向站点添加了一个新的URL,类似于
http://mysite.com/newsfeed.php
。您的用户可以从JavaScript代码中调用它,如下所示:

http://mysite.com/newsfeed.php?callback=someFunction
您的服务器将返回文本:

someFunction('[{"title" : "news item title", "body" : "news item body here"},
               {"title" : "2nd item title", "body" : "2nd item body here"}]');
(显然,对于存储/显示新闻的方式,您会使用任何有意义的JSON结构,但关键是请求中指定的函数名会作为响应的一部分返回。)

输出这样的内容的PHP不是很复杂,如下所示:

<?php header('content-type: application/json; charset=utf-8');

$data = array(array("title" => "news item title", "body" => "news item body here"),
              array("title" => "2nd item title", "body" => "2nd item body here"));

echo $_GET['callback'] . '('.json_encode($data).')';

您的问题太广泛了。首先,您必须考虑如何检索数据(Ajax?)。然后如何解析它,取决于格式,并提取相关信息,然后构建DOM。这就是它的要点。它是来自php的html页面