How To Display Pre Stored Text Using Javascript?

Discussion in 'JavaScript' started by lightlysalted, Nov 30, 2009.

  1. #1
    Hi All,

    I am still at a relatively basic level of knowledge with javascript and need some help.

    I want to display a number of text based adverts on a webpage, (kind of like Google Adsense does it, except i want to promote specific products that i have in mind) no more than 5 at a time. However so that i don't have to update all these adverts when they expire on each of my 300 pages of content I would like to store these in a separate file, and just update the file as and when required. I would cut and paste only the javascript code on each page which would access the advert file.

    Essentially I want to use javascript code to access this file and display it on my webpage.

    I have done this with PHP, but my dilemma is that i do not want to have to change all my current pages from .html to .php extensions just to acheive this as it would lose all the thousands of backlinks to my site which is now well established.

    I want to be able to have 5 clickable text links down the page at least all the the same time, I don't want a rotating link or just to display one link.

    Does anyone know of any free code that could achieve this, is it possible?

    I really hope you can help as it's driving me crazy.
     
    lightlysalted, Nov 30, 2009 IP
  2. Amit Oxy

    Amit Oxy Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Store the data into a XML files... Retrive them using Ajax
    Learn AJAX here..
    w3schools.com/ajax/ajax_xmlhttprequest.asp
    then convert this data to html...
    w3schools.com/xml/xml_to_html.asp
     
    Amit Oxy, Dec 1, 2009 IP
  3. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's the code, in three files.

    Place the .js and .css files in a folder named: dynamic_links

    Edit the .js and .css files with Notepad, to suit.

    The .zip file contains a demo.
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>None</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <!--  dynamic links style sheet and script -->
    	<link type="text/css" rel="stylesheet" href="./dynamic_links/dynamic_links.css">
    	<script type="text/javascript" src="./dynamic_links/dynamic_links.js"></script>
    <!--  dynamic links style sheet and script -->
    
    </head>
    	<body>
    		
    		
                       
                  
    	</body>
    </html>
    
    
    
    Code (markup):
    
    	var promotionLink = [];       
    
    	promotionLink['tagline_1'] = "This is the first product"; 
    	promotionLink['link_1'] = "http://news.antiwar.com/2009/10/26/growing-doubts-about-us-as-clinton-heads-to-pakistan/";
    
    	promotionLink['tagline_2'] = "Here's the second product"; 
    	promotionLink['link_2'] = "http://news.antiwar.com/2009/10/26/pakistan-accuses-india-of-backing-taliban/";
    
    	promotionLink['tagline_3'] = "The link to the 3rd product"; 
    	promotionLink['link_3'] = "http://news.antiwar.com/2009/10/26/iran-eyes-compromise-deal-hints-at-endorsement-of-current-offer/";
    
    	promotionLink['tagline_4'] = "The 4th of 5 products"; 
    	promotionLink['link_4'] = "http://original.antiwar.com/news/2009/10/27/resignation-letter-from-us-foreign-service-officer-matthew-p-hoh/";
    
    	promotionLink['tagline_5'] = "The fifth and last product"; 
    	promotionLink['link_5'] = "http://www.msnbc.msn.com/id/33501858/ns/world_news-south_and_central_asia/";
    
    /* Do not edit below this line */
    
    	var linkWindow = "";
    
    	function openProductLink(nLink){	
    		
    		linkWindow = linkWindow.closed ? "" : linkWindow;
    		var currLink = nLink.lastChild.data;
    		for (i=1; i<6; i++)
    			{
    			 if (promotionLink['tagline_'+i] == currLink && linkWindow == "")				
    				{				 
    				 linkWindow = window.open(promotionLink['link_'+i]);				 
    				}	
    			 else if (promotionLink['tagline_'+i] == currLink)
    				{ 
    				 linkWindow.location.replace(promotionLink['link_'+i]);
    				 linkWindow.focus();
    				}				
    			}			
    	}
    
    	function initBannerLinks(){
    
    		var nBody = document.getElementsByTagName('body')[0];
    		var nLinkDiv = document.createElement('div');
    		nLinkDiv.className = "link_container";
    		for (i=1; i<6; i++)
    			{
    			 var nLink = document.createElement('div');
    			 nLink.appendChild(document.createTextNode(promotionLink['tagline_'+i]))
    			 nLink.onclick = function()
    				{
    			 	 openProductLink(this);
    				}
    			 nLinkDiv.appendChild(nLink);
    			}
    		nBody.appendChild(nLinkDiv);		
    	}
    
    	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', initBannerLinks, false) : addEventListener('load', initBannerLinks, false);	
    
    
    Code (markup):
    
    
    	 .link_container 
    		{
    		 border-top: 1px solid black;
    		 border-left: 1px solid black;
    		 border-right: 1px solid black;
    		 background-color: transparent;
    	 	 width: 250px;
    		 height: 114px;
    		 font-family: "times new roman";
    		 font-size: 11pt;
    		 font-weight: bold;
    		 text-align: center;
    		 color: blue;	
    		 cursor: pointer; 		
    		}
    
    	.link_container div
    		{
    		 background-color: #f0fff0;
    		 border-bottom: 1px solid black;
    		 width: 100%;
    		 float: left;
    		 height: 17px;
    		 padding-top: 2px;
    		 padding-bottom: 3px; 
    		 margin-top: 0px;
    		 margin-bottom: 0px;
    		 margin-left: 0px;
    		 margin-right: 0px;	
    		}
    
    Code (markup):
     

    Attached Files:

    Last edited: Dec 1, 2009
    Mike H., Dec 1, 2009 IP
  4. zoneweb

    zoneweb Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Nothin' wrong switchin' to PHP. Just create folders with the exact same names as your HTML files and put index.php's inside the folder.
     
    zoneweb, Dec 1, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Mike H: don't take this the wrong way but your style of programming (even though it may accomplish the objective) is inadequate, particularly so for a distinguished 'javascript author' such as yourself that SELLS his javascript 'demos'.

    I am not basing this observation on the back of this solution alone, I had a good look at your site and the 'sample' provided (the one with the charts). Despite of your seemingly excellent grasp of the language and its elements on a granular level, you seem to really enjoy doing things in a non-scalable and non-semantic way. your solutions may "work" but they do not render themselves well to growth or change or the ability to coexist with other scripts on the page you may have no control of (consider namespacing, onload, co-existing with prototyped arrays and so forth).

    You separate style from content from code and yet fail to separate data from code.

    inline variables everywhere WITHIN functions that users are supposed to modify (until your trademarked '/* Do not edit below this line */' ) -- how bad a practice is that? didn't they teach you function arguments or JSON? :D have you considered that when having the functions defined into a .js file, the data source may be a .php/asp/xml/anything? you say this can be changed, have you considered browser caching of .js? etc etc

    p.s. oh and - browser sniffing by appName?

    http://www.howtocreate.co.uk/tutorials/jsexamples/sniffer.html
    http://www.jibbering.com/faq/faq_notes/not_browser_detect.html
    http://www.quirksmode.org/js/support.html

    etc etc.

    you do well to "despise" change but you'd do worse if you don't objectively follow new trends and adapt. to be honest, none of that would matter if you were not trying to market your work anyway.

    In addendum, a quote of yours: "garbage."
     
    dimitar christoff, Dec 2, 2009 IP
    sarahk likes this.