SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Including a file?

    Hi Folks,

    Is it possible to use an include in na javascript file (.js) ?

    Code:
    v_content=[
    
    ['','Some text goes here, some more text here<br><i>Joe, QLD</i>','_blank'],
    ['','Some more text goes here, some more text here and here<br><i>Bob, Melbourne</i>','_blank'],
    ['','Some text goes here, some more text here<br><i>Bily, Vic</i>','_blank']
    
    ];
    I was hoping to be able to use php / ssi to load the content between the (v_content=[) tags from a text file.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can do something like this:
    PHP Code:
    $str 'v_content=[';
    $str .= file_get_contents('textfile.txt');
    $str .= '];';
    file_put_contents('stuff.js'$str); 
    Then just include the JS file as normal:
    HTML Code:
    <script type="text/javascript" src="stuff.js"></script>
    Though it might be more sensible to simply append $str to an existing JS file that will be used anyway, in order to reduce the number of HTTP requests (which slow down display of a page).

  3. #3
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply raffles
    The code I posted above 'was' already in the .js file.

    So... I dont understand your logic when using:
    file_put_contents('stuff.js', $str);
    <script type="text/javascript" src="stuff.js"></script>

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To run PHP in a JavaScript file simply change the file extension on the file from .js to .php and then include the PHP in the file the same as if it were HTML. As long as the PHP actually writes valid JavaScript code that will then work.
    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="^$">

  5. #5
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    As long as the PHP actually writes valid JavaScript code that will then work.
    Thanks for the great idea, however its not rendering correctly, getting javascript errors

    I have this to deal with in the .js file:

    Code:
    /* USE WORDWRAP AND MAXIMIZE THE WINDOW TO SEE THIS FILE
    ========================================
     V-NewsTicker v2.2
     License : Freeware (Enjoy it!)
     (c)2003 VASIL DINKOV- PLOVDIV, BULGARIA
    ========================================
     For IE4+, NS4+, Opera5+, Konqueror3.1+
    ========================================
     Get the NewsTicker script at:
     http://www.smartmenus.org/
     LEAVE THESE NOTES PLEASE - delete the comments if you want */
    
    // BUG in Opera:
    // If you want to be able to control the body margins
    // put the script right after the BODY tag, not in the HEAD!!!
    
    // === 1 === FONT, COLORS, EXTRAS...
    v_font='verdana,arial,sans-serif';
    v_fontSize='10px';
    v_fontSizeNS4='11px';
    v_fontWeight='normal';
    v_fontColor='#4A49A8';
    v_textDecoration='none';
    v_fontColorHover='#ff0000';//		| won't work
    v_textDecorationHover='underline';//	| in Netscape4
    v_bgColor='none';
    // set [='transparent'] for transparent
    // set [='url(image_source)'] for image
    v_top=-20;//	|
    v_left=-10;//	| defining
    v_width=198;//	| the box
    v_height=75;//	|
    v_paddingTop=2;
    v_paddingLeft=1;
    v_position='relative';// absolute/relative
    v_timeout=2500;//1000 = 1 second
    v_slideSpeed=30;
    v_slideDirection=0;//0=down-up;1=up-down
    v_pauseOnMouseOver=true;
    // v2.2+ new below
    v_slideStep=1;//pixels
    v_textAlign='left';// left/center/right
    v_textVAlign='top';// top/middle/bottom - won't work in Netscape4
    
    // === 2 === THE CONTENT - ['href','text','target']
    // Use '' for href to have no link item
    
    v_content=[<? include"testimonials.txt";?>];
    
    // ===
    v_ua=navigator.userAgent;v_nS4=document.layers?1:0;v_iE=document.all&&!window.innerWidth&&v_ua.indexOf("MSIE")!=-1?1:0;v_oP=v_ua.indexOf("Opera")!=-1&&document.clear?1:0;v_oP7=v_oP&&document.appendChild?1:0;v_oP4=v_ua.indexOf("Opera")!=-1&&!document.clear;v_kN=v_ua.indexOf("Konqueror")!=-1&&parseFloat(v_ua.substring(v_ua.indexOf("Konqueror/")+10))<3.1?1:0;v_count=v_content.length;v_cur=1;v_cl=0;v_d=v_slideDirection?-1:1;v_TIM=0;v_fontSize2=v_nS4&&navigator.platform.toLowerCase().indexOf("win")!=-1?v_fontSizeNS4:v_fontSize;v_canPause=0;function v_getOS(a){return v_iE?document.all[a].style:v_nS4?document.layers["v_container"].document.layers[a]:document.getElementById(a).style};function v_start(){var o,px;o=v_getOS("v_1");px=v_oP&&!v_oP7||v_nS4?0:"px";if(parseInt(o.top)==v_paddingTop){v_canPause=1;if(v_count>1)v_TIM=setTimeout("v_canPause=0;v_slide()",v_timeout);return}o.top=(parseInt(o.top)-v_slideStep*v_d)*v_d>v_paddingTop*v_d?parseInt(o.top)-v_slideStep*v_d+px:v_paddingTop+px;if(v_oP&&o.visibility.toLowerCase()!="visible")o.visibility="visible";setTimeout("v_start()",v_slideSpeed)};function v_slide(){var o,o2,px;o=v_getOS("v_"+v_cur);o2=v_getOS("v_"+(v_cur<v_count?v_cur+1:1));px=v_oP&&!v_oP7||v_nS4?0:"px";if(parseInt(o2.top)==v_paddingTop){if(v_oP)o.visibility="hidden";o.top=v_height*v_d+px;v_cur=v_cur<v_count?v_cur+1:1;v_canPause=1;v_TIM=setTimeout("v_canPause=0;v_slide()",v_timeout);return}if(v_oP&&o2.visibility.toLowerCase()!="visible")o2.visibility="visible";if((parseInt(o2.top)-v_slideStep*v_d)*v_d>v_paddingTop*v_d){o.top=parseInt(o.top)-v_slideStep*v_d+px;o2.top=parseInt(o2.top)-v_slideStep*v_d+px}else{o.top=-v_height*v_d+px;o2.top=v_paddingTop+px}setTimeout("v_slide()",v_slideSpeed)};if(v_nS4||v_iE||v_oP||document.getElementById&&!v_kN&&!v_oP4){
    document.write("<style>.vnewsticker,a.vnewsticker{font-family:"+v_font+";font-size:"+v_fontSize2+";color:"+v_fontColor+";text-decoration:"+v_textDecoration+";font-weight:"+v_fontWeight+"}a.vnewsticker:hover{font-family:"+v_font+";font-size:"+v_fontSize2+";color:"+v_fontColorHover+";text-decoration:"+v_textDecorationHover+"}</style>");v_temp="<div "+(v_nS4?"name":"id")+"=v_container style='position:"+v_position+";top:"+v_top+"px;left:"+v_left+"px;width:"+v_width+"px;height:"+v_height+"px;background:"+v_bgColor+";layer-background"+(v_bgColor.indexOf("url(")==0?"-image":"-color")+":"+v_bgColor+";clip:rect(0,"+v_width+","+v_height+",0);overflow:hidden'>"+(v_iE?"<div style='position:absolute;top:0px;left:0px;width:100%;height:100%;clip:rect(0,"+v_width+","+v_height+",0)'>":"");for(v_i=0;v_i<v_count;v_i++)
    v_temp+="<div "+(v_nS4?"name":"id")+"=v_"+(v_i+1)+" style='position:absolute;top:"+(v_height*v_d)+"px;left:"+v_paddingLeft+"px;width:"+(v_width-v_paddingLeft*2)+"px;height:"+(v_height-v_paddingTop*2)+"px;clip:rect(0,"+(v_width-v_paddingLeft*2)+","+(v_height-v_paddingTop*2)+",0);overflow:hidden"+(v_oP?";visibility:hidden":"")+";text-align:"+v_textAlign+"' class=vnewsticker>"+(!v_nS4?"<table width="+(v_width-v_paddingLeft*2)+" height="+(v_height-v_paddingTop*2)+" cellpadding=0 cellspacing=0 border=0><tr><td width="+(v_width-v_paddingLeft*2)+" height="+(v_height-v_paddingTop*2)+" align="+v_textAlign+" valign="+v_textVAlign+" class=vnewsticker>":"")+(v_content[v_i][0]!=""?"<a href='"+v_content[v_i][0]+"' target='"+v_content[v_i][2]+"' class=vnewsticker"+(v_pauseOnMouseOver?" onmouseover='if(v_canPause&&v_count>1){clearTimeout(v_TIM);v_cl=1}' onmouseout='if(v_canPause&&v_count>1&&v_cl)v_TIM=setTimeout(\"v_canPause=0;v_slide();v_cl=0\","+v_timeout+")'":"")+">":"<span"+(v_pauseOnMouseOver?" onmouseover='if(v_canPause&&v_count>1){clearTimeout(v_TIM);v_cl=1}' onmouseout='if(v_canPause&&v_count>1&&v_cl)v_TIM=setTimeout(\"v_canPause=0;v_slide();v_cl=0\","+v_timeout+")'":"")+">")+v_content[v_i][1]+(v_content[v_i][0]!=""?"</a>":"</span>")+(!v_nS4?"</td></tr></table>":"")+"</div>";v_temp+=(v_iE?"</div>":"")+"</div>";document.write(v_temp);setTimeout("v_start()",1000);if(v_nS4)onresize=function(){location.reload()}}

  6. #6
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres the error in action :
    http://www.goonlinewebdesign.com.au/...mo10/index.php

    Can anyone possibly help?

  7. #7
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error is stemming from the 'include' code.

    v_content=[<? include"testimonials.txt";?>];

    Code:
    ['','Some text goes here, some more text here<br><i>Joe, QLD</i>','_blank'],
    ['','Some more text goes here, some more text here and here<br><i>Bob, Melbourne</i>','_blank'],
    ['','Some text goes here, some more text here<br><i>Bily, Vic</i>','_blank']
    when I removed '<? include"testimonials.txt";?>' from my code I got no errors, but obviously the script wasnt working.
    Does anyone have any ideas?

  8. #8
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i fixed it!!
    I ended up putting the contents of the original .js file straight into the .php page instead of using the javascript include 'src' and it rendered the js and included the txt file perfectly

  9. #9
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good Work!

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Sounds like you didn't set up the separate JavaScript file to be parsed for PHP first - if the code just got passed to the JavaScript instead of being run on the server that would explain why the separate file didn't work.

    Did you rename the file and use:

    <script type="text/javascript" src="stuff.php"></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="^$">


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
  •