Jump to content

Maximus

Members
  • Posts

    11
  • Joined

  • Last visited

Posts posted by Maximus

  1. Yeah, I've run into this problem before. The following does not work:
    var div = document.getElementById("Container");div.innerHTML = "<script>alert('hello!');<\/script>";

    It's possible that it is because of browser security, I'm not certain. It does work, however, if you use the method I described in my previous post (i.e. the eval(script.innerHTML)).

    Hmm, I tried your code and my browser froze. I'm assuming its because of the loop :SHowever, I found another way to accomplish my goal. By inserting an image into the ajax loaded content I was able to add the onLoad trigger to it. Example:
    <img src="test.gif" onLoad="do(this);" style="display: none;">

    Its not a perfect solution as it is sort of primitive, but it seems to work really well and is fairly easy to use. The additional style code hides it. Small 1x1 images are perfect for this :)EDIT: In FireFox all is fine, but in IE(6) the function thats with the image is being called endless times...for whatever reason :-( I have yet to figure out how to fix this.

  2. Thanks a lot for such a responsive post!My explanation wasn't too good, but I think you understood me.I don't understand why having this (below) in the content that is being loaded, isnt working.

    <script type="text/javascript">go('resources.ic', 'resources');<\/script>

    I could try your first suggestion oh just modifiying the function that loads the content to call another funtion, but then that would only be a temporary solution and really woulnt help me that much. I need the content that is loaded via AJAX to call a function and there seems to be no way of doing this.

  3. I am currently doing a lot of work with AJAX based web applications, and I find it to be a real drag that you cannot use the "onLoad" event to call functions when loading sections of a page with AJAX.My problem is that I have to execute a piece of code as soon as the page is loaded (dynamically, using ajax). On normal sites, this would work with the onLoad property, so once the page has loaded, that event would trigger a JS function of my choice. But with AJAX, only a part of the site "refreshes/loads" so this event handler is useless.I have tried simply putting the function I want to execute upon loading in script tags like so:

    <script type="text/javascript">loadContent('pagename', 'divname');</script>

    but with no success :-(Does anyone know how to solve this problem? This is really important to me, and I would really appreciate any help, thanks.

  4. Using an event handler to handle the onreadystatechange event isn't all that complicated. Here's an example:
    // first, make sure that the ajax object is global:var xmlhttp;// load your ajax object however you normally would.// then do all your ajax stuff here...xmlhttp.onreadystatechange = changeText;xmlhttp.open(....);xmlhttp.send('');// this is the event handler...function changeText(){	if(xmlhttp.readyState == 4) // 4 = complete	{		if(xmlhttp.status == 200) // Http Status Code 200 (OK)		{			// you can either directly modify the innerHTML of the 			// elements right here			document.getElementById("mydiv").innerHTML = xmlhttp.responseText;			// or you can call a new function:			// myNewFunction(xmlhttp.responseText);		}	}}

    When the readystate has changed, your changeText function will fire. This will happen up to 4 times (once for each readystate). When the readystate is 4 (complete), you can opt to check to make sure that the response returned a 200 (OK) rather than something like a 404 (File Not Found) or a 500 (Internal Server Error). If readystate = 4 and status = 200, your code will execute.The onLoad event only fires when the page loads - the only way you can use onLoad to run your code would be to refresh the entire page which would defeat the purpose of using AJAX.I hope this helps.

    Hmm, thanks.
  5. Are you trying to run changeText in order to send the AJAX request? Or are you wanting changeText to run when the AJAX request is finished?If you want it to run the AJAX, you can do something like this:
    window.onload = changeText;

    or

    <body onload="changeText();">

    If you want it to run when the AJAX request has finished, use the onreadystatechange event:

    xmlhttp.onreadystatechange = changeText;xmlhttp.open(....)xmlhttp.send(....)

    After its finished loading...I know I can use onreadystate, but thats too complicated, because this whole thing is inside a HUGE JS function. Isen't there another way, using simple html dom?I mean, it loads html objects via ajax instead of refreshing the page, but I don't see why "onLoad" doesnt work for those objects as it does when you refresh the whole page.
  6. Hey, I am using AJAX to request a page, so I cannot use "body onload", because the page is loaded into an existing HTML page.Instead, I tried using the following in my ajax-loaded page:

    <script type="text/javascript" onLoad="java script:changeText()"></script>

    I tried that because in the w3schools js tutorial it said that the onLoad event works with <script>. http://w3schools.com/jsref/jsref_onload.aspAnyways, my code didn't do anything...the function itself works though, I tested it with a link and "onmouseover".I just need the function "changeText" to be executed as soon as the page is loaded. Keep in mind im using ajax, so the page isent actually refreshed.Help is greatly appreciated!

  7. Yes that worked! However, one last thing.I used the firstChild.data before because it added the "new text" (req.responseText) to the existing div. The code you gave me merly clears the div and displays the text.I tried going:

    var element = document.getElementById("response_register");element.innerHTML = req.responseText + element;element.style.color = "#ff0000";

    That displays the text and "[object HTMLDivElement]" (no quotes). Ugh.

  8. The innerHTML property is the way to go: http://www.w3schools.com/htmldom/prop_anchor_innerhtml.asp
    <div id="response_register" onclick="this.innerHTML = 'new-text'">default-text</div>

    To change the color of the element as well, you can do something like this:

    <div id="response_register" onclick="updateElement()">default-text</div><script type="text/javascript">function updateElement(){	var element = document.getElementById("response_register");	element.innerHTML = "new-text";	element.style.color = "#ff0000";}</script>

    Not really what I'm looking for...see this is all part of a bigger JS function that submits a form via ajax and displays the result.document.getElementById("response_register").firstChild.data=req.responseText;document.getElementById("response_register").innerHTML.style.color="#FF0000"; I just need to change the color of "req.responseText". I tried the above code, but it doesnt work, just displays the text returned by the server, WITHOUT color.
  9. Most of the tutorials at w3schools are exellent (php, html), however the DOM ones are really bad, in my opinion, because they fail to explain it properly. At least I was really confused after reading through it.All I want to know, is how to change the text in a div..., the simplest thing, and that isen't even on w3schools (correct me if I am wrong).

    <div id="response_register">default-text</div>document.getElementById("response_register").firstChild.data='new-text';

    The above will output (when used in/with a JS function):new-textdefault-textThis is what I want, but I when I use html formatting like <font color=red>Hello</font> it displays the html code as well, without changing the color. Anyone know how to do that? I also experimented with the innderHTML property, to no success.

×
×
  • Create New...