Zend passing server url / parameter to javascript file - javascript

For my application I need to pass a parameter through zend to my exter javascript file.
I need the server url in the javascript with the language parameter.
my javascript is placed in /js/javascript.js
I've tried the follow
layout.phtml
$this->headScript()
->prependScript('BASE_URL = "test";')
->appendFile('/js/javascript.js');
javascript.js
var url = BASE_URL
But my firebug console keeps saying that BASE_URL is not defined. What
is the good way to do this?
Regards.
Nicky

First you include javascript.js, and just after it you prepend script before appended javascript.js.
$this->headScript()
->appendFile('/js/javascript.js')
->prependScript('BASE_URL = "test";');

Related

Send arguments to use function in JS Use API | AEM 6.5

I am trying to get the url in my Javascript file. But seems like window.location work in JS Use API neither. So I was trying to send the URL as an argument but that's failing with an error.
My JS code:
"use strict";
use(function () {
var url = this.url;
/other code/
});
My HTML code:
<sly data-sly-use.item="'myfile.js' # url=value">
HTL/Sightly is a server-side template language. The scripts (including JS Use Objects) are compiled and ran once, when the page is rendered. To get the current URL/location you can leverage SlingHttpServletRequest#getRequestPathInfo. The current request is available as request as part of the HTL Global Objects.
Also, regarding getting errors when using <sly data-sly-use.item="'myfile.js' # url=value">, that's most probably because value is not defined as a variable name in the HTL server-side rendering context. Using <sly data-sly-use.item="'myfile.js' # url='https://www.test.com/'"> should work.

Set an url globally in JavaScript

In an ASP.NET MVC application, we call web service and web API methods from JavaScript files. Whenever the url gets changed, we have to modify the url in many .js files.
As we access the url in JavaScript, is there anyway to set it globally like web.config in .NET?
Thanks.
You can just have this or something similar in your view:
<script>
window.apiUrl = '<%=ConfigurationManager.AppSettings["apiUrl"]%>';
</script>
(or if it's Razor?...)
<script>
window.apiUrl = '#(ConfigurationManager.AppSettings["apiUrl"])';
</script>
That way, your other scripts can simply reference:
var url = window.apiUrl;
You can declare a global variable in the view(call VewBag in the Controller),and This variable in the configuration

How do I get jQuery ajax url prefixes to resolve controller action methods?

I'm sure this question has been asked multiple times, but the one example of the problem I'm having was asked about here.
Basically, if I have an ajax call to /Controller/Action, it works fine if I'm using my local development server of localhost/Controller/Action.
If I publish to UAT, the url needs more information. It's now server/application/Controller/action, which obviously breaks my ajax call.
<%: Url.Action("MyAction") %> solves this, but doesn't exist in the context of a seperate javascript file.
Currently, I have a javascript variable 'urlPrefix' in my app.js file, which I have to change between ""and "applicationName" everytime I debug locally or release to a different server.
What's the easiest way of solving this?
You can use:
#Url.Content("~")
to resolve the root path of the application, so there'll be no need to change it between deployments.
One way to implement this is to add the following to your <head> in _layouts:
<script type="text/javascript">
var rootPath = '#Url.Content("~")'; // includes trailing /
</script>
(use your namespace as required)
then you can generate your actions such as:
var url = rootPath + 'controller/action/' + id

How to update JavaScript variable using PHP

I have a JavaScript file named a pricing.js which contains this content in it:
var price_arr = new Array('$ 16.95','$ 30.95','$ 49.95','$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95');
But I want to to update this part of JavaScript file using PHP so please help me in this how can I update this part of the content from JavaScript file using PHP?
'$ 16.95','$ 30.95','$ 49.95','$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95'
Please understand that Javascript is Client Side code and PHP is server side code.
There can be 2 possible ways or scenarios which you want to achieve:
If you want to manipulate the JS file before sending it to client, you can rename the Javascript file to have .php extension and write php code to provide a variable. For Ex:
<?php
// write php code to create a string of values using a for loop
$price_array_string = "'$ 16.95','$ 30.95','$ 49.95',
'$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95'";
// Javscript Code var price_arr = new Array(`<?`php echo $price_array_string ?>);
The other alternative is that you get the value of price array by making an Ajax Request to a PHP web service.
Simply give the pricing.js file a .php extension. You can then include PHP in the javascript file the way you would for any other page. Just be sure your HTML code specifies that it's still "text/javascript" when you load it. You'll probably want to set the content-type in the PHP file as well, like so: header("Content-type: text/javascript");
Using your PHP script you can put a value in an HTML5 data attribute of an element on your page and then read it from your JavaScript, e.g. <body data-array="'$ 16.95','$ 30.95'"> can be generated with PHP, and then read with JS: var are = new Array($('body').attr('data-array').split(',')). Here jQuery is used in order to read the attribute value, but you can also do it with pure JavaScript.

Partial url in CodeIgniter

Fellow Coders,
so far I have been using the CI url helper to build full urls using base_url() and site_url(). Now I'm trying to access a controller function within some javascript code that will be loaded as a js file.
it's actually an ajax call with a url parameter that should be something like:
url : '/account/check_user'
now unless i prefix the url with the full path as ins http://servername/..... the code fails.
all the code examples i've seen use the short version of the url but i cannot get it to work. I'm sure this is really simple but i'm stuck.
i could pass a hidden form field to the js code but i'd rather not. any ideas?
thanks
Well, I also tend to use absolute URLs and a good practice I always do is declaring a JS variable:
var base_url = "<?php echo base_url(); ?>";
In:
the head section
as the first line of my script tag
if I have a main.js file that holds most of my JS code AND it's always included in my views, then I put that line first thing in the file.
After that, you use it like:
url : base_url + 'account/check_user'
Anyway, the first slash / in your url tells the browser to go to the URL root which would not be the right place to put your url chunk in! for example:
if your CI installation is in ci folder and your URL is: domain.com/ci/contorller/method/
Then your URL will become: domain.com/contorller/method/!!
I'm usually assigning the base_url() to a JS variable right in the head to have it available to all methods. Something like this
<script type="text/javascript">
var baseUrl = "<?php echo base_url() ?>";
</script>
You should actually be using CI's site_url() function. base_url() is useful for generating a URL to a resource (such as script or stylesheet), but site_url() is the best choice when generating a URL to a page within the app itself, such as when making an Ajax request, as described.
So the best code to use would be something along these lines:
<script type="text/javascript">
var site_url = "<?php echo site_url() ?>";
</script>

Categories

Resources