elflacodepr Posted March 15, 2009 Share Posted March 15, 2009 Hello all, I'm doing a website using only client-side languages, I thought might be a good idea to use Javascript to read a file which contains News about the website...Is there any way I can do this? if so, can you give an example, I'm not good with JS Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted March 15, 2009 Share Posted March 15, 2009 what type of file do you wish to read using javascript? And is it on the same server? Quote Link to comment Share on other sites More sharing options...
elflacodepr Posted March 15, 2009 Author Share Posted March 15, 2009 I want to read an HTML containing the news and yes, the file will be in same server Quote Link to comment Share on other sites More sharing options...
corbin Posted March 16, 2009 Share Posted March 16, 2009 The only way that it's possible to read a server file client side is to use AJAX. Quote Link to comment Share on other sites More sharing options...
elflacodepr Posted March 16, 2009 Author Share Posted March 16, 2009 alright...so how I can do it? I mean...the scripting Quote Link to comment Share on other sites More sharing options...
corbin Posted March 16, 2009 Share Posted March 16, 2009 AJAX isn't something I can explain in 2 minutes, so I suggest you read a tutorial or two. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted March 16, 2009 Share Posted March 16, 2009 Ajax is indeed the way to go. Here is an old Ajax topic but it still works http://www.phpfreaks.com/forums/index.php/topic,115581.0.html Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2009 Share Posted March 16, 2009 Or, Create those "files" as javascript and then simply include them using a javascript SRC attribute. Besides, AJAX requires server-side code and you already stated you are only using client-side code. Example: main.htm <html> <head> <script type="text/javascript"> window.onload = function() { document.getElementById('events').innerHTML = eventsHTML; } </script> <script src="events.inc.js"></script> </head> <body> Here are today's events: <div id="events"></div> </body> </html> events.inc.js //Create the events html code as a variable //var eventsHTML; var eventsHTML = ''; eventsHTML += '<ul>'; eventsHTML += '<li>Underwater basket weaving</li>'; eventsHTML += '<li>Naked decathalon</li>'; eventsHTML += '<li>Hula Hoop Football</li>'; eventsHTML += '</ul>'; Resulting page will show ================== Here are today's events: Underwater basket weaving Naked decathalon Hula Hoop Football Of course, using javascript for something like this (including AJAX) is a poor solution. This really should be implemented server-side. But, you can also check if your server supports Server Side Includes (SSI) which will allow you to include an HTML file inside another HTML file. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 17, 2009 Share Posted March 17, 2009 "Besides, AJAX requires server-side code and you already stated you are only using client-side code." AJAX in no way requires server-side code. AJAX requires server side code no more than Internet Explorer or Firefox does ;p. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 17, 2009 Share Posted March 17, 2009 AJAX in no way requires server-side code. AJAX requires server side code no more than Internet Explorer or Firefox does ;p. Ah, yes. I was thinking about the "reading" of a file and didn't consider XML. You could, of course, read/parse an XML file with JavaScript. If there is a way to read/parse fiels with AJAX that are not XML, please elaborate. I still contend that a server-side language is the way to go due to cross-browser issue whenever dealing with javascript. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.