SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    capturing javascript code IN javascript variables.

    Hi guys,

    I recall reading somewhere in the O'Reilly Javascript (4th ed) book, that the following may be possible. Couldn't find it after a solid hour of hunting for it.

    Q: Is it possible to store large wadges of javascript code IN a javascript variable (without having to escape it and saving it in a string)? For example, is it possible to output 1000 lines of js code to a textarea using only js?

    I need some way of referring to chunks of javascript code within javascript itself... a variable is the only way I could think that it MAY be possible.

    Thanks! :-)

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure!
    Code:
    <html>
    <head>
    	<title>Test</title>
    	<script type="text/javascript">
    		function showScript( id, ta )
    		{
    			ta.value = document.getElementById( id ).innerHTML.strip();
    		}
    		String.prototype.strip = function()
    		{
    			return this.replace( /^\s+|\s+$/g, "" ).replace( /\t/g, "   " );
    		}
    	</script>
    	<script id="showMe" type="text/javascript">
    		function blah()
    		{
    			var a = "yadda yadda";
    			var b = "blah blah"
    			alert( [a,b].join( "\n" ) );
    		}
    	</script>
    </head>
    <body>
    	<form>
    		<input type="button" value="Show Script" onclick="showScript( 'showMe', this.form.output )" />
    		<input type="button" value="Run Script" onclick="blah()" />
    		<br />
    		<textarea name="output" rows="10" cols="70"></textarea>
    	</form>
    </body>
    </html>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's absolutely wonderful! I've been thinking about this for literally hours & didn't think it possible!

    Thanks VERY much. I'll play around with the code and try a few things out...

    Wonderful!!! :-) :-)

  4. #4
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another way to do it is...

    To define a function (or array of functions) and simply reference the function (or indexed array) identifier without any trailing parentheses. I use this method in my HTML() bookmarklet library. It is truly the crux of how it all works...

    I have five factory functions defined in array x...
    Code:
    function HTML(p){
     if(!p){
      /*default*/
      p='A action align alt B background Base bgcolor BIG BLINK BODY border
    bordercolor bordercolordark bordercolorlight Br cellpadding cellspacing
    checked color cols colspan compact content dir DIV enctype face FONT
    FORM H1 H2 H3 H4 H5 H6 HEAD height Hr href hspace HTML I id Img
    Input lang language leftmargin LI marginheight marginwidth maxlength
    Meta method name NOSCRIPT noshade nowrap OL onblur onchange
    onclick onfocus onload onmouseout onmouseover onreset onselect
    onsubmit onunload OPTION P PRE profile readonly rows rowspan SCRIPT
    SELECT size SMALL SPAN src start STRIKE STYLE style SUB SUP TABLE
    target TD TEXTAREA TH TITLE title topmargin TR TT type U UL valign
    value vspace width wrap'
     };
     String.prototype.write=function(){
      document.write(this);
      return this 
     };
     String.prototype.alert=function(){
      window.alert(this);
      return this 
     };
     String.prototype.status=function(){
      window.status=this;
      return this 
     };
     var x=[
     function(W){var q=String.fromCharCode(34);return ' x='+q+((typeof(W)!='undefined')?W:'x')+q},
     function(W){return '<x'+((typeof(W)!='undefined')?W:'')+' />'},
     function(W){return '<x'+((typeof(W)!='undefined')?W:'')+'>'+this+'</'+'x>'},
     function(W){var o='<x'+((typeof(W)!='undefined')?W:'')+' />';return o+this.join(o)},
     function(W){var o='<x'+((typeof(W)!='undefined')?W:'')+'>';var c='</'+'x>';return o+this.join(c+o)+c}
     ];
    
    Later on, I use the implicit string value of the x functions to build a string of generic javascript code.
    Code:
     var j=[];
     var f=0;
     var a=p.split(' ');
     var t,u,l;
     for(var i=0;i<a.length;i++){
      t=a[i];
      u=t.toUpperCase();
      l=t.toLowerCase();
      if(t==l){
       /*attr*/
       j[f]='window.'+u+'='+x[0]  
      }
      else if(t==u){
       /*container;containers*/
       j[f]='String.prototype.'+u+'='+x[2]+';Array.prototype.'+u+'='+x[4]  
      }
      else{
       /*tag;tags*/
       j[f]='window.'+u+'='+x[1]+';Array.prototype.'+u+'='+x[3]  
      };
    After substituting every generic x (not the array identifier), with a specific replacement a[i], I join the array j of my customized javascript statements together (with a semicolon) for one massive eval...
    Code:
      /*specific*/
      j[f]=j[f].replace(/x/g,l);
      f++ 
     };
     window.status='HTML() bookmarklet library: Copyright (c) 2002-'+(new Date()).getFullYear()+
    ', by Richard Edwards. ['+f+' tag/attrs added] ';
     /*implement!*/
     eval(j.join(';'))
    };
    HTML();
    So, you can go both ways.... use a function to build a string, or use a string to evaluate some javascript!

    :ca_redwards.

    Quote Originally Posted by AtomicPenguin
    Hi guys,

    I recall reading somewhere in the O'Reilly Javascript (4th ed) book, that the following may be possible. Couldn't find it after a solid hour of hunting for it.

    Q: Is it possible to store large wadges of javascript code IN a javascript variable (without having to escape it and saving it in a string)? For example, is it possible to output 1000 lines of js code to a textarea using only js?

    I need some way of referring to chunks of javascript code within javascript itself... a variable is the only way I could think that it MAY be possible.

    Thanks! :-)
    :Richard Edwards


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
  •