SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript namespaces linked file problem

    Please take a look at the code, I'm trying to use a JS namespace. Works fine when the JS is coded in the HTML file, however it doesn't work when I try to lift the JS into an external file. Any ideas where I'm going wrong? Thanks.

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My JS Namespace Template Project</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    
    <link href="content.js" type="text/javascript" />
    
    </head>
    <body>
    	<input id='btn01' type='button' value='Do Something' /><p/>
      <div id='content'></div>
    </body>
    </html>
    Code:
    	var CBP = CBP || {};
    	
    	CBP.g_system_name = 'CBP'; 
    	
    	CBP.Show = {
    		
    		Msg: function() {
    			var config = {
    				fname: 'fred',
    				lname: '',
    				age: '24',
    				postcode: 'XXX YYY'
    			}
    			CBP.Detail.Record(config);
    		}
    	}
    	
    	CBP.Detail = {
    		
    		Record: function(p_config) {
    			var fname = p_config.fname;
    			var lname = p_config.lname || 'unknown';
    			var age = p_config.age;
    			var postcode = p_config.postcode;
    			
    			var buf = new CBP.fnStringBuilder;
    			buf.append('System Name: ' + CBP.g_system_name + "<br/>");
    			buf.append('First Name: ' + fname + "<br/>");
    			buf.append('Last Name: ' + lname + "<br/>");
    			buf.append('Age Name: ' + age + "<br/>");
    			buf.append('Postcode Name: ' + postcode);
    			$('#content').html(buf.toString());
    		}
    	}
    	
    	CBP.fnStringBuilder = function() {
    		
    		this.arr = [];
    		this.append = function(p_txt) {
    			this.arr.push(p_txt);
    		};
    		this.toString = function() {
    			return this.arr.join('');
    		};
    	}
    
    	$('document').ready(function() {
    		$('#btn01').click(function() { CBP.Show.Msg(); } );
    	});

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It seems to work for me in separate tests whether in an external file or within the page.

    Can you provide a test page that demonstrates the problem? There may be something else causing an issue that we can resolve.
    Last edited by paul_wilkins; Jun 6, 2012 at 19:50.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    <link href="content.js" type="text/javascript" />

    should be

    <script src="content.js" type="text/javascript"></script>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is really embarrasing, I've been staring at the code for ages, thanks for providing a fresh pair of eyes.
    You are absolutely right, should have used <script not <link.
    Many thanks....!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •