Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
spasticdonkey
RavenNuke(tm) Development Team



Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA

PostPosted: Mon Dec 27, 2010 8:22 am Reply with quote

I was working on converting an inline JavaScript into an external PHP file that generated the JS file dynamically; and need to pass a variable to the script. Was wondering if there is a better way to accomplish this...?

I will be adding this to the addJSToBody array, and want to use a file rather than inline so that I can call this script from blocks as well, without potentially loading the script twice. I've already retrieved $disqus_shortname from the db @ this point and done some validation.

Code:
addJSToBody('includes/jquery/disqus.php?ds='.$disqus_shortname, 'file');


I was hoping to not have to include any core files in my php file that generates the JS, so I can't use check_html or any of the other RN functions. The variable I'm passing should contain no spaces; only letters, numbers, and hyphens...

Code:
Header('content-type: application/x-javascript');

$ds = $_GET['ds'];
if (!preg_match('/[^0-9A-Za-z-]/',$ds)){
echo 'var disqus_shortname = \''.$ds.'\';
   (function () {
      var s = document.createElement(\'script\'); s.async = true;
      s.type = \'text/javascript\';
      s.src = \'http://disqus.com/forums/\' + disqus_shortname + \'/count.js\';
      (document.getElementsByTagName(\'HEAD\')[0] || document.getElementsByTagName(\'BODY\')[0]).appendChild(s);
   }());';
   }


Any pointers?
 
View user's profile Send private message Visit poster's website
Palbin
Site Admin



Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Mon Dec 27, 2010 11:45 am Reply with quote

Code:


include(xxxxxxx.php);
disqus($disqus_shortname);


Code:


function disqus($ds) {
   Header('content-type: application/x-javascript');
   if (!preg_match('/[^0-9A-Za-z-]/',$ds)){
      echo 'var disqus_shortname = \''.$ds.'\';
      (function () {
         var s = document.createElement(\'script\'); s.async = true;
         s.type = \'text/javascript\';
         s.src = \'http://disqus.com/forums/\' + disqus_shortname + \'/count.js\';
         (document.getElementsByTagName(\'HEAD\')[0] || document.getElementsByTagName(\'BODY\')[0]).appendChild(s);
      }());';
   }
}

_________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. 
View user's profile Send private message
Palbin







PostPosted: Mon Dec 27, 2010 11:46 am Reply with quote

I forgot to change the function to write to the body. Also I do not thin you need that header() in there.
 
spasticdonkey







PostPosted: Mon Dec 27, 2010 12:23 pm Reply with quote

That is much better than using $_GET Smile

Although one problem though, unless I'm missing something. I need an external file added to the addJSToBody array, not 'inline' - since 'inline' is not checked for loading more than once...

Code:
function disqus($ds) {

//Header('content-type: application/x-javascript');
if (!preg_match('/[^0-9A-Za-z-]/',$ds)){
   $CCount = 'var disqus_shortname = \''.$ds.'\';
       (function () {
           var s = document.createElement(\'script\'); s.async = true;
           s.type = \'text/javascript\';
           s.src = \'http://disqus.com/forums/\' + disqus_shortname + \'/count.js\';
           (document.getElementsByTagName(\'HEAD\')[0] || document.getElementsByTagName(\'BODY\')[0]).appendChild(s);
       }());';
addJSToBody($CCount, 'inline');
   }
}
 
Palbin







PostPosted: Mon Dec 27, 2010 1:30 pm Reply with quote

Code:


function disqus($ds) {
   static $disqus_loaded;
   if (isset($disqus_loaded)) return;
   //Header('content-type: application/x-javascript');
   if (!preg_match('/[^0-9A-Za-z-]/',$ds)){
      $CCount = 'var disqus_shortname = \''.$ds.'\';
         (function () {
            var s = document.createElement(\'script\'); s.async = true;
            s.type = \'text/javascript\';
            s.src = \'http://disqus.com/forums/\' + disqus_shortname + \'/count.js\';
            (document.getElementsByTagName(\'HEAD\')[0] || document.getElementsByTagName(\'BODY\')[0]).appendChild(s);
         }());';
      addJSToBody($CCount, 'inline');
      $disqus_loaded = 1;
   }
}
 
spasticdonkey







PostPosted: Mon Dec 27, 2010 5:18 pm Reply with quote

Excelente! Many thanks as always. Encouraged me to read a little about static values too Smile

I'll be using this again, a handy method for module/block combos that rely on the same JavaScript; only loading when needed and not more than once. Assuming your JavaScript doesn't require being loaded in the head of the document.... Wink

For anyone following along or since this could be adapted for other purposes:
I made a slight tweak since it is using the "inline' method (adding script tags)
includes/jquery/disqus.php
Code:
function disqus($ds) {

   static $disqus_laoded;
   if (isset($disqus_laoded)) return;
   if (!preg_match('/[^0-9A-Za-z-]/',$ds)){
$CCount = '<script type="text/javascript">
var disqus_shortname = \''.$ds.'\';
(function () {
   var s = document.createElement(\'script\'); s.async = true;
   s.type = \'text/javascript\';
   s.src = \'http://disqus.com/forums/\' + disqus_shortname + \'/count.js\';
   (document.getElementsByTagName(\'HEAD\')[0] || document.getElementsByTagName(\'BODY\')[0]).appendChild(s);
}());
</script>
';
      addJSToBody($CCount, 'inline');
      $disqus_laoded = 1;
   }
}


then used
Code:
include_once('includes/jquery/disqus.php');

disqus($disqus_shortname);

from within my module and block.. making sure $disqus_shortname had been defined in both cases
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©