You can use the URL helper in Code Igniter to load CSS and Javascript with the base_url() method, but what if you have images dynamically being placed into your HTML via javascript? for example in my javascript file I've got
var arrowimages={down:['downarrowclass', 'images/down.png', 23], right:['rightarrowclass', 'images/right.png']}
and those images will be placed into whatever menu item I've specified has a drop down menu.
but that file is a .js file so obviously the server won't load php inside of it.
so, how can I set the base url for the JS?
Thanks!
-Aaron
Define a constant javascript variable like BASE_URI in the of your view, you can then reference this variable in any of your external javascript files.
<head>
....
<script type="text/javascript">
var BASE_URI = "<?php echo base_url(); ?>";
</script>
....
</head>
External js...
var arrowimages={down:['downarrowclass', BASE_URI+'images/down.png', 23], right:['rightarrowclass', BASE_URI+'images/right.png']}
Related
I am trying to insert my profile from the external website (url) to my blog.
Cross-domain is prohibited so iframe, AJAX and jquery load don't work. PHP file_get_contents() do but dynamically generated content inside page doesn't load.
Help me to understand why and how can I solve this issue?
I use such code:
<head>
<base href="https://bookmate.com/">
</head>
<?php
$url = 'https://bookmate.com/kirillmazur/finished';
echo file_get_contents($url);
?>
this is due to missing of two javascript file. you can try by adding those two files in to your local directiry
<script src="/javascript/core.js?4417dc0"></script>
<script src="/javascript/modules.js?4417dc0">
check all the missing files in local directory usinging browser console
Hope It will help. I had same problem Here is what I did:
I used iframe and javascript to get page with all the dynamically loaded contents
contents =document.getElementById('load_page1').contentWindow.document.documentElement.outerHTML;
I loaded the page in iframe
<iframe id="load_page1"></iframe>
jQuery(document).ready(function(){
var url="www.google.com"
jQuery('#load_page1').attr('src',url);
});
then from Javascript I get contents of that iframe in a variable on button click event and sent it through ajax by encoding it into base64
<input type="button" id="load_css" />
jQuery(document).on('click',"#load_css",function(){
contents =document.getElementById('load_page1')
.contentWindow.document.documentElement.outerHTML;
contents = btoa(contents);
jQuery.ajax({
url:'process.php',
data:{contents:contents},
method:"post"}).done(function(data){
jQuery('.css_target').html(data);
});
});
and on ajax page I did all the processing thorugh decodeing it back from base64 to string.
I am developing a small drawing tool which collects some data from the user before starting the tool. I'm using Symfony2 - the drawing tool is pure javascript.
So once the user reaches the page where the tool loads, javascript needs to know about some of the data the user has entered previously (for example height & width of the canvas), which have been sent to the controller by forms. I have no problem using the variables in the templates, but can't access them with the javascript files.
Is there a way to set javascript variables in a template?
I've included the javascript file in the template like so (which works):
<script type="text/javascript" src="<?php echo $view['assets']->getUrl('bundles/mybundle/js/myjs.js') ?></script>
Just put some script tags in your template and output the variable into them using PHP. It could look similar to this:
<script type="text/javascript">
var canvasHeight = <?php echo $canvasHeight; ?>;
var canvasWidth = <?php echo $canvasWidth; ?>;
</script>
Can't you just print the variable with php in the page? something like
<script type="text/javascript">
var t=<?php echo $var; ?>
</script>
I have a JavaScript file which has a hard coded BASEURL variable, this value is then used by other functions in the file.
I would like for this url value to be set dynamically so that I don't need to manually change it for different installs. Is it possible to insert a PHP variable value into a JavaScript file?
Rather than try to mix PHP into javascript files, here's what I do in the HTML template:
<head>
<script>
var BASEURL = "<?php echo base_url(); ?>";
</script>
<script src="/path/to/my_script1.js"></script>
<script src="/path/to/my_script2.js"></script>
</head>
This sets the variable and allows all your embedded scripts to access it. base_url() would be replaced by whatever method you use to fetch the base url with PHP.
Suppose you have a JS file written on the fly by PHP; file1.php
<?php
header('Content-Type: application/javascript');
$var = $_GET['var1'];
echo "jsvar = ".$var.";";
?>
The client-side source code of file1.php?var1=10 will then be
jsvar=10;
There exists several ways:
Load the baseurl via AJAX, but maybe this is to slow, maybe you need it earlier.
let the javascript file running through the php parser. Then you could use inline echos.
The most convenient and easiest way, is to make a <script>var baseurl = '...';</script> in the html/php output of your page.
You will need to make your file a php file, not a js file. From there you can include any PHP tags in the file to work your dynamic magic. The key to make this whole thing work is in the headers, which you will need to set like so:
<?php
Header("content-type: application/x-javascript");
echo 'var js_var = ' . $php_var;
?>
alert (js_var);
This technique can be used to for CSS files as well.
Heredoc worked for me today:
<?php
echo <<<ANYNAME
<script LANGUAGE="JavaScript" type="text/javascript">
<!--
// code ...
var myLatlng = new google.maps.LatLng($lat, $lon);
// code cont. ...
//-->
</script>
ANYNAME;
?>
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
php variable in html no other way then: <?php echo $var; ?>
I am wondering about how people go about using CodeIgniter and jQuery for AJAX.
In your AJAX request you have
{
url : ???,
data : {a:1,b:2},
success: ....
}
So, how do you build the URL?
Do you
have all your JavaScript in your view files, and just use site_url() to build the URL
have all your JavaScript in external js files, have a header view you include that has something like <script>var base_url = '<?php echo site_url(); ?>';</script>. Then in your external js files have url: base_url+'rest/of/path/';
some other method?
I have my all my js in an external file and load it in my template.
For specific ajax requests, just call the page as you normally would.
$.ajax({
type: 'POST',
url: '/ajax/login',
data: blabla,
success: function(data) {
// do something
},
dataType: 'json');
});
In answer to your question, I've had no need to specify the base url, as putting '/' before the controller name sets the root of the site automatically. You could also use ../ etc
if you are writing your ajax in external file then you can define your base url in the view file like
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
Then on your external ajax file write
url : base_url+'controllerName/functionName',
It can also be done by loading a view that contains your JavaScript.
I currently load JavaScript from a view at the end of the rendered page. Since it is a PHP file with html <script> in it, you can use the URL helper functions like site_url() to generate the URLs you need for each function.
An example view might contain:
<script>
$.ajax{
url : "<?=site_url("controller/function")?>",
data : {a:1,b:2},
}
</script>
That will get you CodeIgniter generated URLs for your JavaScript. You could even pass variables into the view for more control over your js.
Usually I make a small javascript in my header, in which I create a base_url and site_url variable (usually being properties of an object I name CI, but that's a personal preeference). I fill these values by echoing the values with PHP. If you make that the first loaded script, you'll always have the site_url available in JS.
Since I'm on mobile I can't post the source now.
I have a really simple requirement but cant find a good solution to it.
How do I get config data into javascript files within magento. I need to load some config data from a module and make it available from within some javascript code. Without loading this data onto hidden fields on the page or using ajax to fetch them from a controller - how do i do this?
Thank you in advance for any help.
You can grab the value you are looking for like this:
// using seo as an example. look at the path field on core_config_data
$value = Mage::getStoreConfig("web/seo/use_rewrites");
Put this in a template that will echo your code:
<?php
$seo_value = Mage::getStoreConfig("web/seo/use_rewrites");
?>
<script type='text/javascript'>
magento_config = {}
magento_config['seo_value'] = "<?php print $seo_value; ?>";
// ... add more ...
</script>
Add that template to your layout and you will now have a global JS object called magento_config that you can use to grab your values. Depending on where you put your template, you may have to wait for the page to load before your variables are available.
Make sure not to echo these values indiscriminately, as there may be security implications in echoing store config data to the frontend.
Hope that helps!
Thanks,
Joe
Add this code in before you load the JS...
<script type="text/javascript">var var_name = <?php echo $var_name; ?></script>
Then you can load in JS like
<script type="text/javascript" src="/js/file.js"></script>
In your file.js (providing that the file is loaded after the variable definition) you can do something like...
//execute as soon as DOM is loaded
window.onDomReady(onReady);
//do on ready
function onReady()
{
alert(var_name);
}
Or if you were using jQuery
$(document).ready(function(){ alert(var_name); }