Use JavaScript to add weather display ideas and code to the page

Source: Internet
Author: User
Tags stringbuffer

  Add weather to the page there are many ways to display, in this article for you to introduce the use of JS easy to implement, the specific code below, interested friends do not miss

Copy code code as follows: <%@ page language= "java" pageencoding= "UTF-8"%>  <html>  <head>  < script>  function Load (CID)   {  var xmlhttp;  if (window. XMLHttpRequest)   {//code for ie7+, Firefox, Chrome, Opera, safari  xmlhttp=new XMLHttpRequest ();  else  {//code for IE6, ie5  xmlhttp=new activexobject ("Microsoft.XMLHTTP"); } -Xmlhttp.open ("Get" , "date.jsp?cid=" +cid,false);  xmlhttp.send ();  var obj = eval ("(" + xmlhttp.responsetext+) "); //var Obj=json.parse (Xmlhttp.responsetext); IE8   document.getElementById ("test"). innerhtml=obj.weatherinfo.city+ ": +obj.weatherinfo.weather1+" "+ obj.weatherinfo.temp1; }  </script>  </head>  <body>  <p id= "test" > Weather </p>  <button id= "btn1" Onclick=load ("101280601") > Shenzhen weather </button>  <button id= "Btn2" Onclick=load ("101250501") > Chenzhou weather </button>  <!--&nbsP City ID get:http://blog.csdn.net/zgyulongfei/article/details/7956118 -->  </body>  </html >    date.jsp  Copy code code as follows: <%@ page language= "java" import= "java.net.*,java.io.*" pageencoding= " Utf-8 "%>  <%  String cid = request.getparameter (" cid ");  url url = new URL (" http://m.weather.com.cn /data/"+cid+" ". html");  httpurlconnection httpconn = (httpurlconnection) url.openconnection ();  Httpconn.connect ();  inputstream cin = Httpconn.getinputstream ();  BufferedReader reader = new BufferedReader (New InputStreamReader (CIN, "UTF-8"));  stringbuffer sb = new StringBuffer ();  String RL = null;  while (RL = Reader.readline ())!= null)   Sb.append (RL);  out.println (SB); %>   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.