Ajax, UTF-8 or gb2312 eval or execScript

Source: Internet
Author: User

Two problems:
Question 1:
The responsetext of XMLHTTP is always encoded with UTF-8 by default. To solve this problem at minimal cost some time ago, the entire project was simply encoded with UTF-8. There are not many solutions on the Internet, and they are all messy. UTF-8 cannot be used in the future.
Question 2:
The Javascript script for loading pages in XMLHTTP cannot be executed. The event driver is still available. To avoid it some time ago, I simply wrote all the scripts together. It's disgusting to load hundreds of k js files every time.

Now we have time to solve this problem in the Orthodox way,
XMLHTTP, originally used to operate XML, responsetext returned things, the use of online binary hard encoding to change the UTF-8 to gb2312 encoding method is naturally not reliable, and if the processing of other encoding is powerless. The ixmlhttprequest object returned by responsexml depends on the XML encoding declaration, which naturally cannot be garbled. No reason.
<? XML version = "1.0" encoding = "gb2312"?>
<Body>
<! [CDATA [
Here will be the HTML text I need,
]>
</Body>
The script can write a row like this;
Returnvalue = xmldom.doc umentelement. text;
Returnvalue is the HTML text I need. It is relatively hard to encode in byte streams. Why not?
The first question is basically solved, regardless of the encoding you use, UTF-8, gb2315.gbk, or 8859-1 to change the Document declaration.
The first problem is that XML is used, so the second problem can be easily solved. Simply analyze our requirements and execute a page script, since it is on the load page, it is generally in two places, the declaration before loading HTML and the call at the completion of loading. For the use of the page, use DHTML event-driven, then change the XML structure.
<? XML version = "1.0" encoding = "gb2312"?>
<Content>
<! -- The pre-defined script needs to be loaded on the page -->
<Onstart>
<! [CDATA [
// The script is written in the head;
]>
</Onstart>
<! -- HTML content -->
<Body>
<! [CDATA [
Htmlcode
]>
</Body>
<! -- The script that needs to be loaded on the page -->
<Onend>
<! [CDATA [
// The script here is equivalent to the script written after the body;
]>
</Onend>
</Content>
Process the script and try to write these lines;
Beginscript = xmldom.doc umentelement. selectsinglenode ("onstart"). text;
Htmlcode = xmldom.doc umentelement. selectsinglenode ("body"). text;
Endscript = xmldom.doc umentelement. selectsinglenode ("onend"). text;
It seems that there is no problem in IE, but an error is reported in Firefox. the hateful Firefox is actually textcontent, And the selectsinglenode method does not work either. The method recognized by the browser can be copied everywhere. Now I only care about IE and Firefox, and I only have these two on the machine. Code ;
The first three lines below are copied from the Netease blog. Find Netease if you have any questions. I am too lazy to write it on my own, let alone Let me find it online.
VaR isie = (document. All & document. getelementbyid &&! Window. Opera )? True: false;
VaR ismozilla = (! Document. All & document. getelementbyid &&! Window. Opera )? True: false;
VaR isopera = (window. Opera )? True: false;

If (isie ){
Beginscript = xmldom.doc umentelement. getelementsbytagname ("onstart"). Item (0). text;
Htmlcode = xmldom.doc umentelement. getelementsbytagname ("body"). Item (0). text;
Endscript = xmldom.doc umentelement. getelementsbytagname ("onend"). Item (0). text;
} Else {
Beginscript = xmldom.doc umentelement. getelementsbytagname ("onstart"). Item (0). textcontent;
Htmlcode = xmldom.doc umentelement. getelementsbytagname ("body"). Item (0). textcontent;
Endscript = xmldom.doc umentelement. getelementsbytagname ("onend"). Item (0). textcontent;
}
Complete. The rest is how to execute it.
Three methods can be used to dynamically execute a script.
A) eval () method of global image in Javascript;
B) execScript () method in DHTML window;
C) New Function () in Javascript; object;
The three methods have their own advantages and disadvantages,
First, the context of the script uses the context when calling. Obviously, there is a scope problem. The scope after execution is only in the called function or method body. Disgusting problem;
Second, it runs on the DHTML top-level image window without scope issues, but the execScript () method has browser compatibility problems. It is only a proprietary method of IE. It cannot be used on Firefox;
Third, in addition to inconvenience, it must be used to declare a method. If you only need to declare a variable, it is also a very annoying problem.
If there is no browser compatibility problem, the second option is the best choice.
The third option is not considered.
First, it is best to solve the scope problem.
The methods I have come up with are as follows;
We declare a variable on the webpage,
That is, VAR author = "show individual ";
Or declare a function.
That is:
Function getblogurl (){
Return author + "blog address: http://shizhong8841.blog.163.com ";
}
At this time, we are equivalent
Window. Author = "show individual ";
Window. getmyblogurl = function (){
Return this. Author + "blog address: http://shizhong8841.blog.163.com ";
}
Then, we only need to slightly expand the window object.
Window. runscript = function (STR ){
Eval (STR );
}
Next, just use this. a = 0; or this. funname = function (arg0) {}; this method is used to write scripts and call runscript (STR); the method is equivalent to defining global attributes and methods on the page, that is, we can open up scope issues to achieve our goal.
Now all the problems are solved, and all the tests passed in internetelpxerer 6.0 and firefax 2.0. The code written is too messy and won't be pasted up. It is a shame, but the idea should be correct.
More questions.

Related Article

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.