faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 50 people (56%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

JavaScript: SRC: Variable: Create: document.write: Can you create a variable 'SRC' statement?

Apr 8th, 2008 00:01
ha mo, Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 30 November 2004 - 00:06 am -------------------
JavaScript: SRC: Variable: Create: document.write: Can you create a 
variable 'SRC' statement?
---
The trick is to escape (that is using a '\' in front of it) the '<' and
the '>'.
(note: you do actually not have to do it for every '<', and '>', but it
is a general method, and it will not do harm).
---
So if you have a lot of SRC files, you can use e.g. a loop to
dynamically generate the SRC filenames.
---
---
e.g.
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
<!-------------------------------------------------------------------->
 document.write( "\<SCRIPT" );
 document.write( "   SRC='myincludefile.js'" );
 document.write( "\>" );
 document.write( "\</SCRIPT\>" );
<!-------------------------------------------------------------------->
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
---
---
e.g.
--- cut here: begin --------------------------------------------------
<SCRIPT>
 var filenameGS = "arraylayer";
 var totalGI = 6;
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT>
  document.write( "\<SCRIPT" );
  document.write( '   LANGUAGE="JavaScript"' );
  document.write( "   SRC='" + filenameGS + "columnheader.js'" );
  document.write( "\>" );
  document.write( "\</SCRIPT\>" );
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT>
  var I = 0;
  var minI = 1;
  var maxI = totalGI;
  for ( I = minI; I <= maxI; I++ ) {
   document.write( "\<SCRIPT" );
   document.write( '   LANGUAGE="JavaScript"' );
   document.write( "   SRC='" + filenameGS + I + "1.js'" );
   document.write( "\>" );
   document.write( "\</SCRIPT\>" );
   //
   document.write( "\<SCRIPT" );
   document.write( '   LANGUAGE="JavaScript"' );
   document.write( "   SRC='" + filenameGS + I + "2.js'" );
   document.write( "\>" );
   document.write( "\</SCRIPT\>" );
  }
</SCRIPT>
<!-- ------------------------------------------------------>
--- cut here: end ----------------------------------------------------
---
---
The latter above replaces thus dynamically the following static
Javascript:
--- cut here: begin --------------------------------------------------
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayercolumnheader.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer11.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer12.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer21.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer22.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer31.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer32.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer41.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer42.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer51.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer52.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer61.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
<SCRIPT
  LANGUAGE="JavaScript"
  SRC="arraylayer62.js"
>
</SCRIPT>
<!-- ------------------------------------------------------>
--- cut here: end ----------------------------------------------------
---
---
Internet: see also:
---
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com
How to document.write SCRIPT sections?
http://www.faqts.com/knowledge_base/view.phtml/aid/841
----------------------------------------------------------------------