jquery.i18n.properties not working - javascript

I have a problem using this jQuery internationalization. I added the jquery.i18n.properties.js, jquery.i18n.properties-min.js, and jquery-min.js files to my resources folder.
In my jsp I added this file
<script type="text/javascript" src="<c:url value="/resources/js/jquery-min.js/>"></script>
<script type="text/javascript" src="<c:url value="/resources/js/jquery.i18n.properties.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/js/jquery.i18n.properties-min.js" />"></script>
Then in the JS file I'm trying to load the .properties file
$(document).ready(function(){
jQuery.i18n.properties({
name:'Messages',
path:'webapp/WEB-INF/messageSources/',
mode:'both',
language:'en',
callback: function() {
alert(jQuery.i18n.prop('registration.field.empty'));
}
});
but it prints out the key ('registration.field.empty') as a message instead of the value.
My property file is: Messages_en.properties

In the first line of your jsp you have this:
<c:url value="/resources/js/jquery-min.js/>"
But it needs to be this:
<c:url value="/resources/js/jquery-min.js" />
You just made a typo... and I'm not familiar with jsp but don't you have to escape the double quotes with \ before each one?

Seems that the WEB-INF directory is restricted to be accessed from outside of the servlet-context. And javascript is definitely out of the servlet-context.
You need to copy your MessageResouces-files (language-files/properties) from WEB-INF into another directory,...
e.g. create directory /i18n (on the same level as WEB-INF, but not inside WEB-INF!)
Put properties files into the created directory
Retry to access the files

Related

Using ResolveClientUrl() in external JavaScript files

I found that ResolveClientUrl() works as expected when JavaScript is embedded in ASPX page, however it doesn't resolve anything when external script is referenced in ASPX like this:
<script src="../Javascript/sessionManagement.js" type="text/javascript"></script>
Is there a way to make ResolveClientUrl() work in external JS files? I was expected that it would because this file IS included in ASPX page but this is not the case.
I found a rather ugly workaround - to include external JS file in ASPX page like this:
<script type="text/javascript" language="jscript">
<!--#include file="../Javascript/sessionManagement.js"-->
</script>
It works (ResolveClientUrl() actually resolves path) but I've never seen external JS files referenced like that and am not sure this is the way to go.
<script type="text/javascript">
var myUrl = '<%= ResolveClientUrl("MyURL") %>';
</script>
<script type="text/javascript" src="../JavaScript/sssionManagement.js"></script>
and in your sessionManagement.js, you can simply use the myUrl variable above

jquery not running with rails in external js file

i am using rails and trying to call jquery code present in external js file whose path i have specified correctly in my html file.The jquery code is executing correctly when i include it directly in html file.
my test.js
$(function(){
alert("yes");
});
and index.html.erb :
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="/assets/javascripts/views/test.js"></script>
</head>
jquery executes when included inside html
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
alert("yes");
});
</script>
</head>
the test.js path is correct since other javascript functions are getting called from the same file but not jquery code
why you have head tag in index.html.erb file . It already included in layout/application.html.erb file . in this way you are including jquery file twice. If you include two versions of jquery in any html they always create issue.
In my suggestion you need to remove your head tag from index file completely . And what ever js file you want to include, include it in your application.js file. for current scenario you can do it like below.
//= require views/test
#user3946910
Try like this.
in your index.html
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="/assets/javascripts/views/test.js"></script>
</head>
and in your test.js file add this.
$(document).ready(function(){
alert("yes");
});
Like this just import all plugins in the html itself, and create fresh .js page to write just your own script.
I hope it will help.

javascript to external .js file

Can't figure out the proper syntax to place this javascript code into a .js file.
<script type='text/javascript' src='http://hostedusa3.whoson.com:8080/include.js?domain=www.yourdomain.com'></script>
<script type='text/javascript' >
if(typeof sWOTrackPage=='function')sWOTrackPage();
</script>
If you want only the inline code inside a separate file, then all you have to do is move the contents of the second script tag into a .js file. Then, set the src attribute to the second <script> to reference the new file's URL (relative or absolute).
external.js:
if(typeof sWOTrackPage=='function')sWOTrackPage();
in index.html:
<script type="text/javascript" src="external.js"></script>
If you want the other stuff in there to. Use your browser. Goto: http://hostedusa3.whoson.com:8080/include.js?domain=www.yourdomain.com copy that into external.js

can't embed some text in js file

My problem is, that I have created a js file. In this file are some text defined.
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
}
Now I want to embed categorie_one in a other js file. That I'm doing with that code:
channel.categorie_one;
But it shows in console: Cannot read property 'categorie_one' of undefined, logical I have linked the file...
Im including the js files in the index.html
<script src="javascripts/default.js" type="text/javascript" rel="javascript"></script>
<script src="javascripts/resources.default.js" type="text/javascript"></script>
In the default.js I have a methode to load the site.
function ChannelLoad(listview) {
//there should be cateogrie_one
}
could you help me pls. Thanks in advance
add a semicolon to your variable definition:
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
};
syntactically your variable definition is a statement; if placed in a row with other statements (i assume that's where it will end up after inclusion of your js files), they have to be separated by semicolons.
In your html file include the scripts in the right order:
<script type="text/javascript" src="defines_object.js"></script>
<script type="text/javascript" src="uses_property.js"></script>
Make sure the object isn't defined in a function or the scope of the object will be limited to that function.
If you defined the channel variable in the resources.default.js file and you are trying to access it in the default.js file, you have to reverse the order in which you link the JS files.
Try:
<script src="javascripts/resources.default.js" type="text/javascript"></script>
<script src="javascripts/default.js" type="text/javascript" rel="javascript"></script>
This code should work.
The only thing you must be aware of is the order you include the files, all files will be executed in the order of their includes, so you should include first the file declaring the object, containing
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
}
then include the second file, using channel.categorie_one;
If this still doesn't work, please post your whole code, there's probably a scope issue (channel is declared in a local scope)
You should include your scripts in the right order:
<script type="text/javascript" src="defines_object.js"></script>
<script type="text/javascript" src="uses_property.js"></script>

cant get jquery to work with php file

I have a linux webserver that has /var/www configured in the Apache2.conf file as the DocumentRoot. Next I have my jquery core file located at /var/www/js/jQuery_v1.4.2.js (a central location for all my websites to access.
The .php index file is located at /var/www/AOI/aoiparse.php, where the aoifunctions.js file is also located. My <head> tag looks as follows:
<script type='text/javascript' scr='/js/jQuery_v1.4.2.js'></script>
<script type='text/javascript' scr='aoifunctions.js'></script>
my aoifunctions.js file has the following in it in order to verify that the script link works:
$(document).ready(function(){
alert("hello");
});
My problem is that I cannot get the alert() to work. I'm not getting an error message so I do not know where the problem is.
You can try <script type='text/javascript' src='../js/jQuery_v1.4.2.js'></script>
(The attribute is src, not scr, plus your path was incorrect)
put the jquery file in /var/www/AOI/js/ and then
remove the first /
<script type='text/javascript' src='js/jQuery_v1.4.2.js'></script>
so it lies in a subdirectory to your main page
or use this if you want to leave it where it is:
<script type='text/javascript' src='../js/jQuery_v1.4.2.js'></script>
You misspelled "src" in your <script> tags. It's short for SouRCe.
<script type='text/javascript' src='/js/jQuery_v1.4.2.js'></script>
<script type='text/javascript' src='aoifunctions.js'></script>
If you used Firebug you'd have been able to easily tell that the scripts weren't loaded.

Categories