george Posted April 4, 2011 Share Posted April 4, 2011 I could use iFrame for this, but i don't want to. I have a div I want to fill with content from a file on my servre. My hunch is that somehow I can var meBox = document.getElementById('maincntnt');var meContent = http://gdawsonportfolio.com/What.txt;meBox.innerHTML = 'meContent'; That middle line is the issue. Is there a way I can do this? A proper syntax or another approach?If there is, my next question is, can I load this file while no events are being triggered or acted upon. IOW, I want to load my content without interferring with client requesting an action. Maybe this is a jQuery goodieTIA Link to comment Share on other sites More sharing options...
ShadowMage Posted April 4, 2011 Share Posted April 4, 2011 You can use AJAX. I'm not sure how it would handle a text file, but you could always save it as a PHP file even if there isn't any PHP code in it. Link to comment Share on other sites More sharing options...
jeffman Posted April 4, 2011 Share Posted April 4, 2011 AJAX can send a GET request and receive the entire contents of a text file as a string. No server-side scripting required. Link to comment Share on other sites More sharing options...
Ingolme Posted April 4, 2011 Share Posted April 4, 2011 I don't think AJAX can get content from another server. You would need a PHP proxy to get the content for AJAX. Link to comment Share on other sites More sharing options...
george Posted April 4, 2011 Author Share Posted April 4, 2011 You can use AJAX. I'm not sure how it would handle a text file, but you could always save it as a PHP file even if there isn't any PHP code in it.I could. But I have not employed php for this site, nor am I using forms. I'm pretty sure I can get the content of a remote file using JavaScript and the full url to the file. After all, we can import java script from a file on another server, and images too. I have used ajax and php to accomplish this in the past. Doing this with php also allows for site scraping, where you catch the content of a remote site's HTML to a variable in php, and then parse that variable to get what you want. But that's hacking, and I avoid it. (It's not hacking when you get a file from your own server.) I just gotta have the shortcut. I'm sure there is a way to assign the content of a remote file to a JavaScript variable directly, without using ajax or php. If I don't get a solution today, I'll use that approach.Thanks Link to comment Share on other sites More sharing options...
jeffman Posted April 4, 2011 Share Posted April 4, 2011 AJAX 2 will allow cross-site requests. Current AJAX does not. Without using an iFrame, JavaScript has no other way to do a thing like this. Even then, iFrame security has increased lately. I don't remember if you can access the content of an iFrame when the source is another site. If you can, an invisible iFrame wouldn't be a terrible way to go. Link to comment Share on other sites More sharing options...
george Posted April 4, 2011 Author Share Posted April 4, 2011 I don't remember if you can access the content of an iFrame when the source is another site.In my case the files I want are on my own server. I could simply include the content I want as HTML in the Javascript file itself, and assign it to a variable to fill an innerHTML request. I'm looking for the most efficent method. I will try the W3Schools example at http://www.w3schools.com/ajax/tryit.asp?fi...e=tryajax_first, Thanks again Deirdre's Dad Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.