jquery load () method usage instructions

Source: Internet
Author: User

The recent site to make some changes, need to refer to another site's page, and then used the jquery box plus the Load method. It's very convenient.

The instructions in the Manual for Load (URL, [data], [callback]) are straightforward:

Loads the remote HTML file code and inserts it into the DOM. Use the Get method by default-automatically converts to POST when additional parameters are passed. In JQuery 1.2, you can specify a selector to filter the loaded HTML document, and only the filtered HTML code will be inserted into the DOM. Syntax is like "URL #some > selector".

But because it's encoded with UTF-8 when it transmits data, so when the call page encoding is not utf-8, there will be garbled, and the filter behind the URL can only start with ID, if the content of the filter is not set ID is very maddening, I hope in the future version will support more filters.

Load page full content:

The code is as follows Copy Code

$ ("#html"). Load ("ajax_load.html");

Load part of page:

The code is as follows Copy Code

$ ("#html"). Load ("ajax_load.html #left");

Loads the entire contents of the page, sends additional parameters as post, and prompts you after a successful load:

The code is as follows Copy Code

$ ("#html"). Load ("ajax_load.html", {"Sort": "desc"}, function () {
Alert ("Ajax_load.html load success!");
});


the Jquery.load method can keep the load down.

The code is as follows Copy Code

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
int i = Integer.parseint (Request.getparameter ("I") ==null? " 0 ": Request.getparameter (" I "));
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


<base href= "<%=basePath%>" >
<title> User Login </title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<script type= "Text/javascript" src= "<%=path%>/scripts/jquery-1.5.1.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#h <%= I%>"). Click (function () {
$ ("#d <%= I%>"). Load ("<%=basepath%>index.jsp", {i:<%=i+1%>});
});
});
</script>

<body>
This is index.jsp?i=<%= i%>
<input type= "button" value= "get<%= i+1%>" id= "h<%= I%>" >
<br>
<div id= "d<%= i%>" style= "border:1px"; Border-style:solid; margin:5px; " >load index.jsp?i=<%= i+1%> here!</div>
</body>

Load does not cache method


1.jQuery provides a way to prevent Ajax from using caching,

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
$.ajaxsetup ({
Cache:false//close AJAX Cache
});
</script>

This method executes every time the load page is executed, otherwise it will only be valid for the first time. Other load also reads cache pages

2. The second is to modify the load load URL address

For example, add a time parameter to the URL, this method can be wap1.0 again I used to avoid reading the cache

Load Chinese garbled solution method

The normal invocation method is simple

The code is as follows Copy Code

$ ("#index_foot_container"). Load ("foot.html");

Analysis, the general cause of load garbled is due to the two page encoding does not affect, the processing method is

1. My Display page index.html encoding is uft-8;
2. My static page foot.html coding is uft-8;

All right, let's summarize the above to introduce an example article comment instance


Second, related pages: (two pages under the same directory)
1, article.aspx article detailed display page

HTML page structure:

The code is as follows Copy Code
<input id= "TopicID" type= "hidden" value= "<%=topicid%>"/>
<div id= "Articlecontent" ></div>
<div id= "Loadarticlereply" ></div>

2. Articlereply.aspx Comments Load page (Show page)

HTML page structure: A Repeater control to display comment content
Loading data in pageload, passing through subsequent post TopicID

Third, the relevant code:

1, the article.aspx page loading time
First step:

The code is as follows Copy Code

The public int topicid;//defines the variable, and then Page_Load the value of the TopicID at load time, and to bind the article
TopicID = Int. Parse (request.querystring["ID"]);//need to be processed, otherwise there will be a lot of problems, here I simply as a demo

Step Two: Load the article comment method:

  code is as follows copy code
<script Type= "Text/javascript"
    function LoadData () {
         var tid = $ ("#TopicID"). Val ();
        $ ("#LoadArticleReply"). Load ("/articlereply.aspx", {"ID": tid}, function () {
            $ ("#LoadArticleReply"). FadeIn ("slow");
       }
       );
   }
</script>

This time we take a look at this loaddata function, take the ID number of the current article, and then according to the Div ID to load the address of the comment, the ID number of the article, as well as a way to display and speed, 
through the load URL and ID, We can go to the ID number of the article on the articlereply.aspx  page to get the relevant comment and then render it in the DIV with ID loadarticlereply.
 
Third step: Load article comments for the first time:
 
This time   article.aspx when the initial load, if the binding article comments?? In the CS and how to call JS?? The
is, of course, the Article.aspx.cs Page_Load,

The code is as follows Copy Code
if (! Page.IsPostBack)//First load article comment, execute with script
{
Clientscript.registerstartupscript (Clientscript.gettype (), "MyScript", "<script>loaddata ();</script> ")//problem: This is a small problem when used with the login control in asp.net2.0, as described at the end of this article
}


Fourth step: When commenting, submit comments and load comments

"Submit comments please see this site jquery Ajax introduction and the use of asp.net in the simple example"
1. Submit Comments:

The code is as follows Copy Code
function SubmitData () {
var id = $ ("#TopicID"). Val ();
var commname = $ ("#txtNickName"). Val ();
var Commemail = $ ("#txtEmail"). Val ();
var Commurl = $ ("#txtUrl"). Val ();
var comment = $ ("#ArticleComment"). Val ();
$.ajax ({
Cache: "False",
Type: "POST",
URL: "/postcomment.aspx",
Data: "id=" + ID + "&name=" + commname + "&url=" + Commurl + "&email=" + commemail + "&comment=" + Comment ,
Success:function (msg) {
if (msg = = "OK") {
Alert ("published successfully!") ");
1, this is the Refresh page all data
Window.location.reload (TRUE);
2, here Refresh Local data
LoadData ();
}
else if (msg = = "Error") {
Alert ("Add failed!") ");
}
}
});
}

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.