|
查看: 1005|回复: 12
|
httprequest的问题
[复制链接]
|
|
|
小弟刚开始学ajax,遇到一些问题,研究了3天还
是不行,想请各位帮帮忙。问题如下:
这是index.html
<html>
<head>
<script type="text/javascript" src="hr.js"></script>这个是httprequest的function
</head>
<body onload="aa()">这是呼叫httprequest的
<div id="aa" />我要加入的代码要放在这里
</body>
</html>
这是我要加进刚才index.html的code(a.html),文件类型a.html或者a.txt都试过了
<script type="text/javascript" src="aaa.js"></script>
其他html tag
结果很明显的a.html有被加进来,被加进来的html tag和css皆能够执行,但是被加进来(蓝字)的javascript却没有执行,过后我将它整个copy n paste开一个新的html档,却能够执行。
<html>
<head>
<script type="text/javascript" src="hr.js"></script>
<link href="css/x.css" rel="stylesheet" type="text/css" />
</head>
<body >
<div>
<script type="text/javascript" src="aaa.js"></script>
<link href="css/x.css" rel="stylesheet" type="text/css" />
其他html tag
</div>
</html>
过后我就试着将上面红字的javascript搬到index.html里,不让他用httprequest加进来,但也是不行,
最后我发现到除非将a.html里的code一开始就放进index.html里,否则用httpresquest中途加进来的话javascript根本不能执行,试过其他javascript也一样。
所以想问下是为什么,有什么解决方法吗?
[ 本帖最后由 y小叶y 于 25-7-2008 09:14 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 26-7-2008 11:02 AM
|
显示全部楼层
原帖由 y小叶y 于 25-7-2008 09:04 AM 发表 
小弟刚开始学ajax,遇到一些问题,研究了3天还
是不行,想请各位帮帮忙。问题如下:
这是index.html
这是我要加进刚才index.html的code(a.html),文件类型a.html或者a.txt都试过了
结果很明显的a.html有被加进 ...
a.html 是什么? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-7-2008 02:14 PM
|
显示全部楼层
原帖由 winmxaa 于 26-7-2008 11:02 AM 发表 
a.html 是什么?
a.html:
<script type="text/javascript" src="c2.js"></script>
<link href="c.css" rel="stylesheet" type="text/css" />
<div id="imageFlow">
<div class="top"></div>
<div class="bank">
<img class="track" src="sb.gif" >
<img class="arrow-left" src="sl.gif">
<img class="arrow-right" src=".gif">
<img class="bar" src="sc.gif" >
</div>
</div> 就是那段js脚本不能执行 |
|
|
|
|
|
|
|
|
|
|
发表于 26-7-2008 02:23 PM
|
显示全部楼层
你好像不是很了解他的 sequence。
基本上你不 呼唤 你的 javascript 它自己怎么会去执行呢?。它都过了那一段。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-7-2008 04:04 PM
|
显示全部楼层
原帖由 tensaix2j 于 26-7-2008 02:23 PM 发表 
你好像不是很了解他的 sequence。
基本上你不 呼唤 你i的 javascript 它自己怎么会去执行呢?。它都过了那一段。。
基本上我有试过用onclick来呼叫他,这是我目前简化过的代码,发现到最后加入的js的确不能执行
httprequest+innerhtml代码
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.6.0");
}
catch(e) { }
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process()
{
if (xmlHttp)
{
try
{
xmlHttp.open("GET", "wp.html", true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch (e)
{
alert("Can't connect to server:\n" + e.toString());
}
}
}
function handleRequestStateChange()
{
myDiv = document.getElementById('myDivElement');
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
try
{
response = xmlHttp.responseText;
myDiv.innerHTML += response;
}
catch(e)
{
alert("Error: " + e.toString());
}
}
else
{
alert("There was a problem retrieving the XML data:\n" +
xmlHttp.statusText);
}
}
}
要加入的代码(wp.html)
<script type="text/javascript">
function a(){
alert("welcome");
}
</script>
index.html
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/interface.js"></script>
<script type="text/javascript" src="load.js"></script>
<link href="css/index.css" rel="stylesheet" type="text/css" />
</head>
<body onload="process()">
<div class="dock" id="dock2">
<div class="dock-container2">
<a class="dock-item2" href="#"><img src="images/home.png" /></a>
<a class="dock-item2" href="#"><img src="images/history.png"/></a>
<a class="dock-item2" href="#"><img src="images/calendar.png" /></a>
<a class="dock-item2" href="#"><img src="images/link.png"/></a>
<a class="dock-item2" href="#"><img src="images/rss.png" alt="rss" /></a>
<a class="dock-item2" href="#"><img src="images/rss2.png"/></a>
</div>
</div>
<div id="myDivElement"/>
</body>
</html>
[ 本帖最后由 y小叶y 于 26-7-2008 04:22 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 26-7-2008 04:58 PM
|
显示全部楼层
<script type="text/javascript">
function a(){
alert("welcome");
}
</script>
把script tag 拿掉。。。
然后
myDiv.innerHTML += response;
之后,加入
eval(response);
在尝试 呼唤
a(); |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-7-2008 05:38 PM
|
显示全部楼层
原帖由 tensaix2j 于 26-7-2008 04:58 PM 发表 
function a(){
alert("welcome");
}
把script tag 拿掉。。。
然后
myDiv.innerHTML += response;
之后,加入
eval(response);
在尝试 呼唤
a();
function a(){
alert("welcome");
出现了这样的文字而已,并不是跳出视窗。
如果放回script tag的话,跳出这样的视窗:Error: SyntaxError: missing } in XML expression
另外,发现到另一段code试过加上去,但不懂如何用。
function LoadJS(fileUrl)
{
var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript= document.createElement("script");
oScript.type = "text/javascript";
oScript.src=fileUrl ;
oHead.appendChild( oScript);
}
function init(){
LoadJS( "./a.js" );
alert( "load success:" + str );
}
[ 本帖最后由 y小叶y 于 26-7-2008 07:44 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 26-7-2008 08:39 PM
|
显示全部楼层
试过把
<script type="text/javascript" src="aaa.js"></script>
放到<head></head>里面吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-7-2008 10:12 PM
|
显示全部楼层
原帖由 max^^ 于 26-7-2008 08:39 PM 发表 
试过把
放到里面吗? 试过了,问题他用到的html tag要同时被载入才可以,就是说js+html要同时被载入,先载入任何一个,另一个用innerhtml来载入的话就不能执行。 |
|
|
|
|
|
|
|
|
|
|
发表于 26-7-2008 11:38 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2008 01:55 AM
|
显示全部楼层
我的aaa.js:) :) :)
var imf = function () {
var lf = 0;
var instances = [];
function getElementsByClass (object, tag, className) {
var o = object.getElementsByTagName(tag);
for ( var i = 0, n = o.length, ret = []; i < n; i++)
if (o.className == className) ret.push(o);
if (ret.length == 1) ret = ret[0];
return ret;
}
function addEvent (o, e, f) {
if (window.addEventListener) o.addEventListener(e, f, false);
else if (window.attachEvent) r = o.attachEvent('on' + e, f);
}
function createReflexion (cont, img) {
var flx = false;
if (document.createElement("canvas").getContext) {
flx = document.createElement("canvas");
flx.width = img.width;
flx.height = img.height;
var context = flx.getContext("2d");
context.translate(0, img.height);
context.scale(1, -1);
context.drawImage(img, 0, 0, img.width, img.height);
context.globalCompositeOperation = "destination-out";
var gradient = context.createLinearGradient(0, 0, 0, img.height * 2);
gradient.addColorStop(1, "rgba(255, 255, 255, 0)");
gradient.addColorStop(0, "rgba(255, 255, 255, 1)");
context.fillStyle = gradient;
context.fillRect(0, 0, img.width, img.height * 2);
} else {
flx = document.createElement('img');
flx.src = img.src;
flx.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(' +
'opacity=50, style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy=' +
(img.height * .25) + ')';
}
flx.style.position = 'absolute';
flx.style.left = '-1000px';
cont.appendChild(flx);
return flx;
}
function ImageFlow(oCont, size, zoom, border) {
this.diapos = [];
this.scr = false;
this.size = size;
this.zoom = zoom;
this.bdw = border;
this.oCont = oCont;
this.oc = document.getElementById(oCont);
this.scrollbar = getElementsByClass(this.oc, 'div', 'scrollbar');
this.text = getElementsByClass(this.oc, 'div', 'text');
this.title = getElementsByClass(this.text, 'div', 'title');
this.legend = getElementsByClass(this.text, 'div', 'legend');
this.bar = getElementsByClass(this.oc, 'img', 'bar');
this.arL = getElementsByClass(this.oc, 'img', 'arrow-left');
this.arR = getElementsByClass(this.oc, 'img', 'arrow-right');
this.bw = this.bar.width;
this.alw = this.arL.width - 5;
this.arw = this.arR.width - 5;
this.bar.parent = this.oc.parent = this;
this.arL.parent = this.arR.parent = this;
this.view = this.back = -1;
this.resize();
this.oc.onselectstart = function () { return false; }
var img = getElementsByClass(this.oc, 'div', 'bank').getElementsByTagName('a');
this.NF = img.length;
for (var i = 0, o; o = img; i++) {
this.diapos = new Diapo(this, i,
o.rel,
o.title || '- ' + i + ' -',
o.innerHTML || o.rel,
o.href || '',
o.target || '_self'
);
}
if (window.addEventListener)
this.oc.addEventListener('DOMMouseScroll', function(e) {
this.parent.scroll(-e.detail);
}, false);
else this.oc.onmousewheel = function () {
this.parent.scroll(event.wheelDelta);
}
this.bar.onmousedown = function (e) {
if (!e) e = window.event;
var scl = e.screenX - this.offsetLeft;
var self = this.parent;
this.parent.oc.onmousemove = function (e) {
if (!e) e = window.event;
self.bar.style.left = Math.round(Math.min((self.ws - self.arw - self.bw), Math.max(self.alw, e.screenX - scl))) + 'px';
self.view = Math.round(((e.screenX - scl) ) / (self.ws - self.alw - self.arw - self.bw) * self.NF);
if (self.view != self.back) self.calc();
return false;
}
this.parent.oc.onmouseup = function (e) {
self.oc.onmousemove = null;
return false;
}
return false;
}
this.arR.onclick = this.arR.ondblclick = function () {
if (this.parent.view < this.parent.NF - 1)
this.parent.calc(1);
}
this.arL.onclick = this.arL.ondblclick = function () {
if (this.parent.view > 0)
this.parent.calc(-1);
}
}
ImageFlow.prototype = {
calc : function (inc) {
if (inc) this.view += inc;
var tw = 0;
var lw = 0;
var o = this.diapos[this.view];
if (o && o.loaded) {
var ob = this.diapos[this.back];
if (ob && ob != o) {
ob.img.className = 'diapo';
ob.z1 = 1;
}
this.title.replaceChild(document.createTextNode(o.title), this.title.firstChild);
this.legend.replaceChild(document.createTextNode(o.text), this.legend.firstChild);
/* ---- update hyperlink ---- */
if (o.url) {
o.img.className = 'diapo link';
window.status = 'hyperlink: ' + o.url;
} else {
o.img.className = 'diapo';
window.status = '';
}
o.w1 = Math.min(o.iw, this.wh * .5) * o.z1;
var x0 = o.x1 = (this.wh * .5) - (o.w1 * .5);
var x = x0 + o.w1 + this.bdw;
for (var i = this.view + 1, o; o = this.diapos; i++) {
if (o.loaded) {
o.x1 = x;
o.w1 = (this.ht / o.r) * this.size;
x += o.w1 + this.bdw;
tw += o.w1 + this.bdw;
}
}
x = x0 - this.bdw;
for (var i = this.view - 1, o; o = this.diapos; i--) {
if (o.loaded) {
o.w1 = (this.ht / o.r) * this.size;
o.x1 = x - o.w1;
x -= o.w1 + this.bdw;
tw += o.w1 + this.bdw;
lw += o.w1 + this.bdw;
}
}
if (!this.scr && tw) {
var r = (this.ws - this.alw - this.arw - this.bw) / tw;
this.bar.style.left = Math.round(this.alw + lw * r) + 'px';
}
this.back = this.view;
}
},
scroll : function (sc) {
if (sc < 0) {
if (this.view < this.NF - 1) this.calc(1);
} else {
if (this.view > 0) this.calc(-1);
}
},
resize : function () {
this.wh = this.oc.clientWidth;
this.ht = this.oc.clientHeight;
this.ws = this.scrollbar.offsetWidth;
this.calc();
this.run(true);
},
run : function (res) {
var i = this.NF;
while (i--) this.diapos.move(res);
}
}
Diapo = function (parent, N, src, title, text, url, target) {
this.parent = parent;
this.loaded = false;
this.title = title;
this.text = text;
this.url = url;
this.target = target;
this.N = N;
this.img = document.createElement('img');
this.img.src = src;
this.img.parent = this;
this.img.className = 'diapo';
this.x0 = this.parent.oc.clientWidth;
this.x1 = this.x0;
this.w0 = 0;
this.w1 = 0;
this.z1 = 1;
this.img.parent = this;
this.img.onclick = function() { this.parent.click(); }
this.parent.oc.appendChild(this.img);
if (url) {
this.img.onmouseover = function () { this.className = 'diapo link'; }
this.img.onmouseout = function () { this.className = 'diapo'; }
}
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2008 01:56 AM
|
显示全部楼层
太长了,上面放不完,继续。
还有这不是我的写的,从网上下载的
Diapo.prototype = {
move : function (res) {
if (this.loaded) {
var sx = this.x1 - this.x0;
var sw = this.w1 - this.w0;
if (Math.abs(sx) > 2 || Math.abs(sw) > 2 || res) {
this.x0 += sx * .1;
this.w0 += sw * .1;
if (this.x0 < this.parent.wh && this.x0 + this.w0 > 0) {
this.visible = true;
var o = this.img.style;
var h = this.w0 * this.r;
o.left = Math.round(this.x0) + 'px';
o.bottom = Math.floor(this.parent.ht * .25) + 'px';
o.width = Math.round(this.w0) + 'px';
o.height = Math.round(h) + 'px';
if (this.flx) {
var o = this.flx.style;
o.left = Math.round(this.x0) + 'px';
o.top = Math.ceil(this.parent.ht * .75 + 1) + 'px';
o.width = Math.round(this.w0) + 'px';
o.height = Math.round(h) + 'px';
}
} else {
if (this.visible) {
this.visible = false;
this.img.style.width = '0px';
if (this.flx) this.flx.style.width = '0px';
}
}
}
} else {
if (this.img.complete && this.img.width) {
/* ---- get size image ---- */
this.iw = this.img.width;
this.ih = this.img.height;
this.r = this.ih / this.iw;
this.loaded = true;
/* ---- create reflexion ---- */
this.flx = createReflexion(this.parent.oc, this.img);
if (this.parent.view < 0) this.parent.view = this.N;
this.parent.calc();
}
}
},
click : function () {
if (this.parent.view == this.N) {
if (this.url) {
window.open(this.url, this.target);
} else {
this.z1 = this.z1 == 1 ? this.parent.zoom : 1;
this.parent.calc();
}
} else {
this.parent.view = this.N;
this.parent.calc();
}
return false;
}
}
return {
create : function (div, size, zoom, border) {
var load = function () {
var loaded = false;
var i = instances.length;
while (i--) if (instances.oCont == div) loaded = true;
if (!loaded) {
instances.push(
new ImageFlow(div, size, zoom, border)
);
if (!imf.initialized) {
imf.initialized = true;
addEvent(window, 'resize', function () {
var i = instances.length;
while (i--) instances.resize();
});
addEvent(document.getElementById(div), 'mouseout', function (e) {
if (!e) e = window.event;
var tg = e.relatedTarget || e.toElement;
if (tg && tg.tagName == 'HTML') {
var i = instances.length;
while (i--) instances.oc.onmousemove = null;
}
return false;
});
setInterval(function () {
var i = instances.length;
while (i--) instances.run();
}, 16);
}
}
}
addEvent(window, 'load', function () { load(); });
}
}
}();
imf.create("imageFlow", 0.15, 1.5, 10); |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2008 08:47 PM
|
显示全部楼层
原帖由 -祯- 于 27-7-2008 12:41 AM 发表 
任何javascript code你用innerHTML写入你的html当中 都是不会被执行的
你可以在after你把你要的code放进了index.html后 再呼叫你的function
一般上我个人的做法
都是把所有要显示的code都放进div后, 再在javasc ... 我用了你的方法也是不行,发现了另一个方法response = xmlHttp.responseText;
sp = document.createElement("script " );
sp.src = "wp2.js";
sp.type = "text/javascript";
//sp.defer = true;
mytag.appendChild(sp);
myDiv.innerHTML += response;
} 用appendchild就能了,现在问题是在ie7不能执行,还我还特地update去ie8也是一样,但在opera,safari,ff却能够run,搞到我头大了
[ 本帖最后由 y小叶y 于 27-7-2008 08:48 PM 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|