Ajax,utf-8 or GB2312 eval or execscript_javascript technique?

Source: Internet
Author: User
Tags cdata
Two questions:
First question:
XMLHTTP responsetext default is always utf-8 encoding, in order to solve the problem with the minimum cost, simply the whole project with UTF-8 code. Online solutions are not many, and are relatively messy, do not like. And we can't use Utf-8 all the time.
Second question:
XMLHTTP the JavaScript script that is loaded into the page and cannot be executed. The driver of the event is still available, and some time ago to avoid it, simply write the script all together. Nausea, always so dry, each time to load hundreds of K of JS files, nausea.

Well, there's time now, the most orthodox way to solve it,
XMLHTTP, is used to manipulate XML, responsetext return things, using the online binary hard code to convert UTF-8 to GB2312 encoding method is inherently unreliable, and if the processing of other coding is powerless. The use of Responsexml returns ixmlhttprequest to the image, dependent on the XML encoding declaration, naturally impossible garbled. There's no reason why not.
<?xml version= "1.0" encoding= "gb2312"?>
<body>
<! [cdata[
Here will be the HTML text I need,
]]>
</body>
The script can write this line;
returnvalue = Xmldom.documentElement.text;
ReturnValue is what I need HTML text, relatively hard to the byte stream to adapt the code, why not?
The first question basically solves, tube you use what code, UTF-8,GB2315.GBK, or 8859-1 change the document declaration can.
The first problem is that XML is used, so the second question is also easy to solve, simply analyze our requirements, execute a page script, by which it is on the loaded page, generally in two places, the HTML of the Declaration before loading and the call of completion. As for the use of the page, use DHTML event-driven, then change the structure of the XML.
<?xml version= "1.0" encoding= "gb2312"?>
<content>
<!--scripts that need to be defined before the page is loaded-->
<onStart>
<! [cdata[
The script here is equivalent to writing in the head;
]]>
</onStart>
<!--HTML content-->
<body>
<! [cdata[
Htmlcode
]]>
</body>
<!--scripts that need to be defined after the page is loaded-->
<onEnd>
<! [cdata[
Here the script is equivalent to write in the body after;
]]>
</onEnd>
</content>
Process the script, try to write these lines;
Beginscript = Xmldom.documentElement.selectSingleNode ("OnStart"). Text;
Htmlcode = Xmldom.documentElement.selectSingleNode ("Body"). Text;
Endscript = Xmldom.documentElement.selectSingleNode ("OnEnd"). Text;
IE seems no problem, but Firefox under the error, hateful Firefox unexpectedly is Textcontent, selectSingleNode method also does not work, the method of browser recognition, anywhere to copy a good. Now I only go to the tube IE and Firefox, my machine is only these two, and then change the code;
The first three lines below are copied from NetEase blog. Have a problem to find netease good. I'm too lazy to write it myself, let alone find it on the Internet.
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.documentElement.getElementsByTagName ("OnStart"). Item (0). text;
Htmlcode = Xmldom.documentElement.getElementsByTagName ("Body"). Item (0). text;
Endscript = Xmldom.documentElement.getElementsByTagName ("OnEnd"). Item (0). text;
}else{
Beginscript = Xmldom.documentElement.getElementsByTagName ("OnStart"). Item (0). textcontent;
Htmlcode = Xmldom.documentElement.getElementsByTagName ("Body"). Item (0). textcontent;
Endscript = Xmldom.documentElement.getElementsByTagName ("OnEnd"). Item (0). textcontent;
}
Complete. The rest is how to do it.
You need to execute a script dynamically, often to three different methods.
(a) The Eval () method of Global pair image in JavaScript;
b the Execscript () method in the DHTML window;
c) The new Function () in JavaScript; to the image;
Three ways, each has its pros and cons,
First, the context of the script uses the context of the call, and there is a significant scope problem, and the execution scope is only within the called function or method. Disgusting problem;
The second, which itself executes on the top of the DHTML window like Windows, does not have a scope problem, but the execscript () method has a browser compatibility problem. Just IE's proprietary method. In Firefox that is not available;
Third, in addition to the use of inconvenience, must be used to declare a method, such as only need to declare a variable, is also a very annoying problem.
If there are no browser compatibility issues to consider, then using the second is the best option.
The third is not to be considered.
The first, if you can solve the scope problem, is the best.
I think of the following methods;
We declare a variable in the Web page,
That is, var author = "Play to disperse people";
or declare a function.
That
function Getblogurl () {
Return author + "blog Address: http://shizhong8841.blog.163.com";
}
At this time, we are equivalent
Window.author = "The play is scattered people";
Window.getmyblogurl = function () {
return this.author + "blog address: http://shizhong8841.blog.163.com";
}
So, let's just extend the window image a little bit.
Window.runscript = function (str) {
eval (str);
}
Magic This is next, as long as the this.a=0; or this.funname = function (arg0) {}; This way script is written and calls RunScript (str); method, it is equivalent to defining the global properties and methods in the page, that is, we can open the scope problem to achieve our goal.
Now, all the problems are resolved, and all the tests are passed under Internetelpxerer 6.0 and Firefax 2.0. The code is too messy, it will not be posted, the province of disgrace, but thought, should still be correct.
More questions, later to say.
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.