JQuery :: Replace Current Html Page With Ajax Response?
Nov 1, 2010
I have page with an Ajax request which returns an entire <hml>..</html> page and I would like to use this response data to replace the current page. I wrote the following :
I 'm having an ajax function that polls a server for a response. This response is in fact an html page which is dynamically constructed on the server. What I want is to be a able to replace my current html page ( not only the body ) with the html I am receiving as response from the server. I tried using document.write or innerHTML on html element but none of these does not seem to work. Here is a sample of the code I am using. code..
You have an AJAX request and the response is mixed HTML + JavaScript. The javascript is referring to elements in the HTML component. Currently the JavaScript gets evaluated first thing when success is triggered and the HTML is returned as a parameter. Obviously this is not going to work ( in this scenario anyway ). As a previous Mootools user I was used to Request.HTML's responseJavaScript parameter and evalScripts option. None of there options are available in the $.ajax implementation.
P.S. I know I could make the javascript a file and load it separately. I know I can make my response JSON and have the js and html in separate properties of the JSON object. I want neither.
I am using an ajax call to generate a new image with some text across it. How can I "replace" the image in the current page with the one I just generated, they will have the same name.
I'm using $.ajax for an ajax request and I've setup a basic html form and if there are errors in the form when the user submits them my server side script is returning them in an array to the client with the errors.
If there are multiple values in the array, how do I display each error on its own line either using <li> tags or even just a <br/>? I'm injecting the ajax response into a div using .html() but how do I iterate the array within that div so I get one error message per line?
Do I need to construct the HTML on the client side after the ajax response has come back or should I do this on the server side before the data is even returned to the client? Right now I'm returning a raw array so that is why I'm asking the question about how to format things up and get the form errors into my div.
I am using jQuery for ajax call and receives HTML as a response.
Response I am getting is
I would like to parse this response and fetch "1","Debopam" and "Poddar" from the response HTML. How to do this and is there any way to parse it using jQuery selector.
I want to use AJAX where the response from server side would be an XML with root element has two divs one for status with values success or failure and other child is HTML which needs to replaced if first div is success.
I need to do is take the current page's url and replace/add a bit into the URL at the appropriate place.For Example, if this is the Current Page URL:http:[url]...I want to be able to use the bookmarklet to change it to:http:[url]....This is all I've got for it so far. Probably botched that up too. : javascript:(function(){var%20a=document.URL.split([url]....
I'm trying to come up with what is probably a kludge. What I'd like to do is take the responseText from an AJAX request -- which will be a full HTML page -- and parse it first to find if there is a <form>...</form> in it. If not I'll display a success message and all is fine, but if it's there, then I want to extract just that form section and display it within a <div> in the page.
This is where my JS skills are failing me. Can anyone point me to the applicable functions, tutorial, or whatever that would show me how to find the <form> and extract it and then replace my div contents with it (just innerHTML?).
I have an ajax post which returns a large html response. It is getting truncated at 98784 characters everytime. Is there a limit to a response size or a way around this?
We have the following situation - when Ajax request is sent what's being returned by the server is usually an XML (which is used for DOM updates) but sometimes it's HTML which is a whole new page that should replace an existing one. I.e when we issue an Ajax request we don't know what will be returned and analyze the response to act accordingly.
Now, the way to replace the current document with a new one used to be easy and portable for both browsers (we're only supporting IE6 and Firefox 1.5):
document.open(); document.write( head ); document.write( body ); document.close();
where "head" and "body" are two parts of the result HTML. We had to cut it to two (rather than going simply with document.write( NewHTML )) because of IE - our head section contains references to external JavaScript files (<script type="text/javascript" src="sth.js"></script>) and IE only loads them when document.write() call ends. So if body contains any script block (<script type="text/javascript".. </script>) using any of JS referenced by head - IE would fail with something like "Resource undefined" if we push the whole new HTMl in one go by using document.write( NewHTML ). But it worked perfectly fine in Firefox, meaning
did the job just fine. What's even more important - it also evaluated all JavaScripts correctly - both in external files referenced by the head and in the JS blocks embedded in the body.
Until Firefox 1.5.0.6/7 where things stopped working completely - our lovely and used-to-be portable code
document.open(); document.write( head ); document.write( body ); document.close();
caused Firefox to loose all CSS/JS and display an HTML only page (as if CSS/JS were disabled). Removing document.close(); improved the situation a bit by displaying the page with CSS this time but still - *no* JavaScript was evaluated (meaning, JavaScript blocks embedded in the document's body were not evaluated).
I took a different path from this point by pushing the new content to document's head and body "innerHTML". It worked but not for JS evaluation - I had to do that manually. To evaluate the JS referenced in the head section of the document - I've traversed the head's DOM tree while looking for the "script" nodes, then downloaded all of them one by one using a synchronous Ajax calls (don't laugh!) and eval()-ed the response. To evaluate JS blocks embedded in the body of the document - I've traversed the body's DOM tree while looking for the "script" nodes and eval()-ed them.
The problem is following: eval()-ing external JS files after downloading them with Ajax doesn't work in 100% of cases - some statements using Prototype functions fail to execute ("prototype.js" is one of external JS files referenced in the head). Anyway, I'm almost sure that what I'm doing is plain wrong, i.e it's sounds silly to download all JS files referenced in the head and eval() them !
So how do I fix it ? Simply put - how do I replce the content of the document to the completely new one received as a response to asynchronous Ajax call ? The new content conatins doctype, head, body - it's a completely new page. And all JS referenced in the head and embedded in the body should be evaluated as well.
I really wish
document.open(); document.write( head ); document.write( body ); document.close();
was working in Firefox as before. Is it simply a Firefox bug, should I submit it ? Btw, document.open.write.write.close() works just fine in IE6.
A couple of months ago I posted a question in these forums pertaining to some trouble I've been having with a webpage utilizing Javascript and PHP to implement AJAX.
Quote: I'm having two Ajax-related problems on a page I am working on at the moment. (Can't include the link since I'm a new Dev Shed member)
On this page, I have two buttons that use Ajax to fetch two separate forms and put them into a chosen div; that part works like a charm.
On said forms, there is a div that is meant to display any necessary error messages when the submit button is clicked. However, on the first time the page is visited and the form is chosen, the div won't display the error message. Only after refreshing the page/choosing the form a second time will the message show. I've been checking the response from the server via Firebug and the response is correct, it just seems like the page isn't updating correctly.
Secondly, once I refresh or choose the form again and the error message begins to show, if I enter values into the form fields and should be getting a different error message, it never updates. Once again, the server response is correct, but for some reason the page just isn't updating to reflect the new message.
I have been using the date field on the New Event form for testing; if you have Firebug, you should be able to see that I am getting the desired response from the server (look in the allErr div), but the HTML isn't changing with it. I have tried both using my own Ajax functions and using jQuery's Ajax implementation, but both give me the same problems. What am I doing wrong?
I had a problem with my previous approach in javascript so i thought of another strategy, to work with java. How can i pass the DOM of the current web page that is the HTML page to java, so that i can parse the DOM and i need to extract the internal contents of the script tags in the page and evaluate them apart from running in the webpage.
How can i pass the dom object to java and evaluate the scripts contents from java.
I am trying to define an "onclick" event to a form button, so that when I click this button, content from another webpage will overwrite this page (not open a new webpage). I've tried the below code (sample) which is not working. <form action="/script" method="post"> <..other elements../> <input type="button" name="jump" value="Jump" /> </form> <script type="text/javascript"> $(':button[name="jump"]').click(function() { $(document).load('/path/to/another_page.html'); }); </script>
I've been learning a lot about jscript and PHP while I've been building my site...now I'm curious about using a little ajax. I have an internal email setup on my site where users can contact one-another...when you click on Inbox, Sent, Drafts, ect it calls a php script and reloads the page. I'm considering for my next release, doing this with jscript but my question is this...
When I print info to the page that I'm on, how hard is it to get rid of the old data that was there? This might be the easiest thing in the world but i don't even know what to call this on google. Also, if that's the norm, how hard is it to add data to a page with out getting rid of whats already there? I've been reading about ajax and I've been using jscripts to check my forms and do simple things but I'm really interested in learning what I really have the power to do here...
1. Take the current URL of the page and open a new window with a URL based on the current page. Some examples (I use "->" to mean "this URL turns into that URL"):I plan to use these bookmarklets in sequence, first pressing 1 to log into the CMS, then pressing 2 to edit the current page.
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me.
It should be fast and if possible compatible with todays modern browser-standards. It should be activated by the onload-event. Code:
I have a URL (var spUrl) and I am trying to get the <title> of the page at that location. I am using $.ajax() to pull the page and process the <title> tag from the response $(data). For some reason, Opera is able pull the title from the <title> tag, but no other browsers are able to do so. When I modify the code to pull the <h2> tag instead of the <title> tag, it works in all browsers.
I need to get all the TABLE HTML code within the <body> tag for a content-generating application. I'm trying to use the html() method, but it only gets the inner HTML. I also tried clone(), but does the same thing. Or maybe I'm using it wrong.
In both the cases I am getting an empty response instead of expected html response.If I just copy paste this adnwurl in browser, I do get a proper html response. Its not working with ajax call.