Declare variable outside of javascript file and use it in file - php

so i am declaring a variable inside of a .php file
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js">
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
But i want to use the variable templatePath inside of jscript_pages.js but when i do it like this my console says: Uncaught ReferenceError...
I hope someone can help :)
Thanks

The content of a script element is alternative content to use if the browser doesn't support src, it isn't a script to run before running the external script.
Use two script elements.
<script>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js"></script>

Set the variable before you reference it in script.
<script>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js">
</script>

As #Quentin said, it's either/or in a single tag.
In concept, you can do what you're looking for by having two script tags
<script type='text/javascript'>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js"></script>
But, in practice what's usually better (for testing, portability, etc) is for your script file to just define functions and then have your on-page scripts (if you have them) call those functions and apply the variables.

In this setup, your function bloginfo('template_directory'); should return a $GLOBALS variable.

Related

How to include php variable in script src

I want to create a variable that holds url to access css and js files. Now I successfully created in config and including it in header for <link rel="stylesheet" href="<?php echo $path; ?>/css/style.css" /> and it just works fine.
But how do I include the same way in the script tag placed in the footer?
In my config.php
$path = "http://localhost/mysite/";
<script>
var path = "<?php echo $path;?>";
</script>
In header :
<link rel="stylesheet" href="<?php echo $path; ?>/css/style.css">
<script src="<?php echo $path; ?>/js/vendor/modernizr.js"></script>//this works, doesn't leave any error.
But in footer:
<script src="<?php echo $path; ?>/js/jquery.myplugin.js"></script>//same style I used in header. But doesn't work!
If pressed f12 in chrome, under sources js files that placed in the footer not loaded but the one placed in header yes!
Just need to remove this part form config
<script>
var path = "<?php echo $path;?>";
</script>
I define it twice
Define a global variable like define('SITE_PATH','http://localhost/mysite/'); in your config file. Use <?php echo SITE_PATH; ?> where ever required.

Why won't my datetime picker work?

I am using code igniter, and I want to add a datetime picker using datetimepicker. The script can be found here
I have 2 sections on my webpage - the header and the content. On the controller, I load them both. Here is my javascript head
<script href="<?php echo base_url(); ?>js/datetimepicker.js" type="text/javascript"></script>
and here is my textbox code
<input id="tanggal" type="text" class="textbox" disabled="disabled"> <img src="<?php base_url(); ?>img/cal.gif">
when I refresh the page, I can hover the cal.gif, but when I click em, cal.gif is not responding. What went wrong in my code?
This is not the correct way of including javascript files:
<script href="<?php echo base_url(); ?>js/datetimepicker.js" type="text/javascript"></script>
It should be:
<script src="<?php echo base_url(); ?>js/datetimepicker.js" type="text/javascript"></script>
Just to keep in mind, base_url allows to do this:
<script src="<?php echo base_url('js/datetimepicker.js')?>" type="text/javascript"></script>

JQmodal pop up can not appear

I've a problem about jqmodal. That pop up can not appear. My scenario is any data in variable warning, pop up will appear and will appear the all warning data.
This the code. Please help me. Thanks
<link type="text/css" rel="stylesheet" href="<? echo base_url().'css/jqmodal/css/prettify.css'; ?>" />
<script type="text/javascript" language="JavaScript" src="<? echo base_url().'css/jqmodal/';?>js/prettify.js"> </script>
<link type="text/css" rel="stylesheet" href="<? echo base_url().'css/jqmodal/';?>css/jquery.modaldialog.css" />
<script type="text/javascript" language="JavaScript" src="<? echo base_url().'css/jqmodal/';?>js/jquery.js"> </script>
<script type="text/javascript" language="JavaScript" src="<? echo base_url().'css/jqmodal/';?>js/jquery.modaldialog.js"> </script>
<script type="text/javascript" language="JavaScript">
$().ready(function(){
prettyPrint();
});
</script>
if(#$warning != NULL)
{
?>
<div id="jqModal" class="jqmWindow" style="display: none;"></div>
<script type="text/javascript">
jQuery().ready(function($){
$('#jqModal').jqm({onShow:setText});
$('#jqModal').jqmShow();
function setText(){
$('#jqModal').text(<?php echo #$warning;?>);
}
});
</script>
<?php
Hey Ayu I built a small demo for you Demo http://jsfiddle.net/BG42j/
Now if you will click on the view... in above demo you will get the text I am setting in my setText function. Rest you can play around with the demo. And please don't forget to accept the answer if this helps. :) Your accept ration is very low :)
Helpful link: http://dev.iceburg.net/jquery/jqModal/
So I reckon issue was the way setText is declared is not correct /* callback executed when a trigger click. Show notice */ onShow (callback): Called when a dialog is to be shown. Be sure to show (set visible) the dialog.
Hope this helps and have a nice one! cheers
Jquery Code
$(document).ready(function() {
$('#dialog').jqm({onShow:setText});
// $('#dialog').jqm().jqmShow({overlay: 70});
});
var setText = function(h) {
/* callback executed when a trigger click. Show notice */
h.w.text('Warning! foooooo');
h.w.css('opacity',0.92).slideDown();
}
​

Problems with quotes when writing javascript code in PHP

I wonder what's the best method for writing javascript code into a PHP variable?
Some times it might be quite long javascript code... Is there a way without escaping all quotes?
<?php
echo '
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>'
?>
Use heredoc. For example:
$var = EOF<<<
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>
EOF;
The EOF can be any arbitrary string you want, it just has to come directly after the <<< delimiter and match on both sides of the string you want to create.
The answer is simple don't mix PHP code and HTML/JavaScript and if you need to do so, end the PHP block with ?> and open it again with <?php after your HTML/JS block.
If you need it inside a variable, you could either use output buffering which is kind of messy though or use Heredoc/Nowdoc strings:
<?php
$foo = <<<FOOBAR
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>
FOOBAR;
?>
Just do:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>
And in case you need php in there somewhere:
a_div.innerHTML = '<iframe style="width:<?php echo $width; ?>;height:300px;" id="iframe_upload" src="index.php">';

jquery and ajax conflict - Joomla

ok Here is my issue: I am using Joomla 1.6,
I have a Ajax JS to display the slide how on my site located: www.dhwnj.com The Jquery script i am using when enabled stops my slide show from working so I tried using the No conflict script but I do not know if I am using it correctly :
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo $this‐>baseurl ?>/templates/<?php echo $this->template ?>/js/jquery.metadata.min.js"></script>
<script type="text/javascript" src="<?php echo $this‐>baseurl ?>/templates/<?php echo $this->template ?>/js/jquery.maphilight.js"></script>
<script type="text/javascript" src="<?php echo $this‐>baseurl ?>/templates/<?php echo $this->template?>/js/fancybox/jquery.fancybox.js"></script>
<script>
jQuery.noConflict();
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
jQuery("jquery.maphilight.js").hide();
});
// Use Prototype with $(...), etc.
$('_class.noobSlide.packed.js').hide();
</script>
<script type="text/javascript" src="<?php echo this->baseurl; ?>/templates/
<?php echo $this->template ?>/js/_class.noobSlide.packed.js" >
</script>
Any Ideas?
Prototype can't work together with Mootools and Mootools is loaded on your page (probably directly by Joomla)
Maybe here's the reason behind your problem...
You should use jQuery after mootools. You can ensure mootools is looaded first by this code (use it in the module or in the view of a component)
JHTML::_('behavior.mootools');
$doc = &JFactory::getDocument();
....
$doc->addScript( "https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" );
$doc->addScriptDeclaration('jQuery.noConflict();');
$doc->addScriptDeclaration("
jQuery(document).ready(function(){
alert('jo');
});"
);
This is code for joomla 1.5, don't know if the functions are the same in 1.6.

Categories