Author |
Message |
spasticdonkey
RavenNuke(tm) Development Team

Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA
|
Posted:
Mon Dec 27, 2010 8:22 am |
|
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? |
|
|
|
 |
Palbin
Site Admin

Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania
|
Posted:
Mon Dec 27, 2010 11:45 am |
|
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. |
|
|
 |
Palbin

|
Posted:
Mon Dec 27, 2010 11:46 am |
|
I forgot to change the function to write to the body. Also I do not thin you need that header() in there. |
|
|
|
 |
spasticdonkey

|
Posted:
Mon Dec 27, 2010 12:23 pm |
|
That is much better than using $_GET
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

|
Posted:
Mon Dec 27, 2010 1:30 pm |
|
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

|
Posted:
Mon Dec 27, 2010 5:18 pm |
|
Excelente! Many thanks as always. Encouraged me to read a little about static values too
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....
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 |
|
|
|
 |
|