Hello I'm using this function as an address book module, for selecting any employee from the sidebar it display all the content of the employee. It works fine in Chrome but not in IE. I'm not able to run the src variables declared in this function in IE. Please suggest me some other ways to declare these type of variables so that these will be compatible to all browsers.
function singleSelect(id)
{
if(flag){
unCheckAll();
userIds="";
//userIds= document.forms['frmSidebarSearch'].elements['userIds'].value + id +",";
var src = ($("#"+id).attr("src") === "<#core.basePath/>images/chk-box-img.gif")
? "<#core.basePath/>images/chk-box-img-tick.gif"
: "<#core.basePath/>images/chk-box-img.gif";
$("#"+id).attr("src",src);
var src2 = ($("#anchor"+id).attr("class") === "")
? "selected"
: "";
$("#anchor"+id).removeClass().addClass(src2);
var elementss = document.getElementById("all").getElementsByTagName('img');
for(i=0;i<elementss.length;i++) {
if($("#"+elementss[i].id).attr("src") === "<#core.basePath/>images/chk-box-img-tick.gif"){
userIds= userIds +"," +elementss[i].id;
}
}
unHilightAll();
highLightIndex(id);
document.forms['frmSidebarSearch'].elements['userIds'].value=userIds;
$('#frmSidebarSearch').ajaxSubmit({target:'#content',url:'<#core.basePath/>sp/manager/manageraddressbook/manager/'+id});
}
flag = true;
}
Have you tried it with double equals (I think triple equals sign is only in languages like php).
(condition == condition) ? true : false;
Related
This question already has answers here:
JavaScript if "x = (a || b || c)" statement not working
(2 answers)
Closed 7 years ago.
My if else is not working like it should. I have 1 if 1 else if and 1 else. When the function runs it executes the if even if the condition is "false".
Here is the JavaScript:
function onSearch(){
var site;
document.getElementById('bar').value = site;
//These are not the actuall links since it's not the actuall code.
if (site === "Google" || "google"){
location.href = "http://www.google.com";
}
else if (site === "Youtube" || "youtube"){
location.href = "http://www.youtube.com";
}
else{
document.getElementById("SearchFail01").innerHTML =
"The country " + site + " does not exist";
}
<!-- Here is the HTML -->
<input type='search' id='bar' list='countries' placeholder='Search..'>
<p id="SearchFail01"></p>
In Javascript, a string in a conditional statement is considered True. The "||" operator won't work the way you're trying to make it work so you'll have to spell it out.
if (site === "Google" || site === "google"){
location.href = "http://www.google.com";
}
else if (site === "Youtube" || site === "youtube"){
location.href = "http://www.youtube.com";
}
else{
document.getElementById("SearchFail01").innerHTML =
"The country " + site + " does not exist";
}
edit:
I also noticed this line:
document.getElementById('bar').value = site;
should probably be flipped if you want to assign bar's value to site
site = document.getElementById('bar').value;
The double pipe doesn't work like you expect. This is how it is supposed to be used.
var foo = someVar || "foo"; not to be used inside an if like that
In your case you could simply lowercase the site and use a single ===
if (site.toLowerCase() === "google") {
location.href = "http://www.google.com";
}
You might also want to consider using a switch.
switch (site) {
case "Google":
case "google":
location.href = "http://www.google.com";
break;
case "Youtube":
case "youtube":
location.href = "http://www.youtube.com";
break;
default:
document.getElementById("SearchFail01").innerHTML = "The country " + site + " does not exist";
break;
}
I believe you have a logic problem if your if and your endif conditions.
When you have 2 or more conditions in JavaScript, separated with the OR (||), or AND (&&) operators you need to make the comparisons in each condition.
Instead of:
if (site === "Google" || "google"){
you have to write:
if (site === "Google" || site === "google"){
And instead of:
else if (site === "Youtube" || "youtube"){
you have to write:
else if (site === "Youtube" || site === "youtube"){
Hope this is helpful!
Cheers mate!
// Function for setting text of an element:
function setText(elementId, message)
{
'use strict';
if ( (typeof elementId == 'string')&& (typeof message == 'string') )
{
var output = $(elementId);
if (output.textContent !== undefined)
{
output.textContent = $(elementId).string;
}
else
{
output.innerText =$(elementId).string ;
}
} // End of main if.
} // End of setText() function.
I need help with this code, I need to define a function name the setText() function as shown below, when I run this code in JS Bin the page shows the code won't run, I couldn't find where the error is. Can anyone give me a hint?
Your type checking for message is unnecessary, but in case you want to keep it:
function setText(elementId, message){
if((typeof elementId == 'string') && (typeof message == 'string')){
document.getElementById(elementId).innerHTML = message;
}
}
setText("foo", "1")
setText("foobar", "2")
setText("bar", "3")
setText("barfoo", "4")
<p id="foo"></p>
<p id="foobar"></p>
<p id="bar"></p>
<p id="barfoo"></p>
You can do it using JavaScript prototypical way. This is little advanced.
HTML:
<span id="spanText"> Your sample Text </span>
First of all augment the type by this code:
/*
Augmenting type
*/
Function.prototype.addMethod = function(funcName, funcBody){
if(!this.prototype[funcName]){
this.prototype[funcName] = funcBody;
return this;
}};
String.addMethod("setText", function(text){
document.getElementById(this).textContent = text;
});
var elementId = "spanText";
elementId.setText("kkkkkkkkkkk");
Load this JS below your HTML file.
You are able to add any of your custom string, number, array method by this way.
See the complete example here:
https://jsfiddle.net/dmsbilas/wu3cd88w/
I want to redirect my users to different languages/subfolders based on their IP address. To do this I use the JavaScript GeoIP API from MaxMind.
The problem: The english speaking people should stay at mydomain.com and not go to mydomain.com/en/. But when I redirect to mydomain.com the GeoIP script runs again which creates an infinite loop.
Here is my code (in index.html for mydomain.com):
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">
var country = geoip_country_code();
if(country == "FR")
{
window.location = "http://mydomain.com/fr/"
}
else
{
window.location = "http://mydomain.com/";
}
</script>
In other posts I read about setting a cookie, but I wasn't able to do it in a way that solves the problem (and it would still create a loop when the user doesn't accept cookies, on mobile for example).
Another solution could be to redirect to mydomain.com/en/ and delete the /en/ folder in the URL via htaccess, but I wasn't able to get this done either.
An example of how I want it to work would be waze.com (it seems like they have the english version in the /en/ folder, but delete it from the URL).
So if anybody is able to help, I would be very grateful. Thanks a lot!
EDIT: I solved the problem myself. It's very simple: Just use the root directory for the english page and change function to "else {null;}" :-)
Your problem is not with geoip but with your code.
Try this:
var country = geoip_country_code();
var currentLocation = String(window.location);
//if geoip is equal FR and window.location is different "http://mydomain.com/fr/"
if(country === "FR" && currentLocation.indexOf("http://mydomain.com/fr/")!==0)
{
window.location = "http://mydomain.com/fr/"
}
//if geoip is different FR and window.location is equal "http://mydomain.com/fr/"
else if(currentLocation.indexOf("http://mydomain.com/fr/")===0)
{
window.location = "http://mydomain.com/";
}
To detect using multiple languages simply edit the following variables:
var defaultsLang are the languages that are supported by the main root (site.com/)
var languages languages supported by sub-pages (site.com/fr/, site.com/es/, etc.)
See code (not tested):
(function(){
var defaultsLang = ["en-us","en"];
var languages = {
"fr": true, //enable french pages
"pt": false, //tmp disable portuguese pages
"es": true //enable spanish pages
};
var country = geoip_country_code().toLowerCase(),
currentLocation = String(window.location),
detectCurrent = function(){
var a = currentLocation.replace(/^(http|https)[:]\/\//, "");
var b = a.split("\/");
b = b[1].toLowerCase();
a = null;
return b.length<5 && (/^[a-z\-]+$/).test(b) ? b : false;
};
var currentLang = detectCurrent();
defaultsLang = "|"+defaultsLang.join("|")+"|";
if(currentLang!==country && typeof languages[country] !=="undefined" && languages[country]!==false){
window.location = "http://mydomain.com/" + country + "/";
} else if(
defaultsLang.indexOf("|" + currentLang + "|")===-1 && //current page is not the same as default languague(s)
defaultsLang.indexOf("|" + country + "|")!==-1 && //geoip is in the list of default language(s)
currentLang!==false
){
window.location = "http://mydomain.com/";
}
})();
i want to add parameters to url, for that i have 2 textbox and a button, i can't figure out where am i stuck and is unable to add parameters to url
here is my code:
HTML:
Param Name: <input id="tbAddParam" type="text" /><br>
Param Value: <input id="tbAddParamValue" type="text" /><br>
<input onclick="javascript:AddParamter();" value="Add" type="button" />
JavaScript:
function AddParamter() {
var new_url = AddUrlParameter(window.location.href, document.getElementById('tbAddParam').value, document.getElementById('tbAddParamValue').value);
window.location.href = new_url;
}
function AddUrlParameter(a, b, c) {
if (b.trim() == "") {
alert("Parameter name should not be empty.");
return a;
}
if (c.trim() == "") {
alert("Parameter value should not be empty.");
return a;
}
if (a.indexOf("?") == -1) {
return a + "?" + b + "=" + c;
}
var d = a.split("?");
if (d.length >= 2) {
if (d[1].trim() == "") {
return d[0] + "?" + b + "=" + c;
}
var e = d[1].split(/[&;]/g);
for (var f = 0; f < e.length; f++) {
var g = e[f]; var h = g.split("=");
if (h.length >= 2) {
if (h[0] == b) {
alert("Url Parameter with provided name already exists! Try Updating that Url Parameter.");
return a;
}
}
}
return a + "&" + b + "=" + c;
}
}
From the comments above:
I've tested your code and works for me (if the page ends in html, jsp or .something). This was tested using Chrome v25.
Later, I've tested on IE9 and it worked after enabling the script execution for local executed pages. A message pops up when you enter to IE. In IE9, it appears on the bottom part saying Internet Explorer restricted this webpage from running scripts or ActiveX controls. and on the right there's this option Allow blocked content..
For IE backward compatibility, it seems that you should replace the d[1].trim() as stated in james emanon's answer.
As an advice, use at least two browsers to test your web pages. And yes, I highly recommend test on IE because it will give you a good(?) feedback for being so sensitive on scripting errors (it will arise so much errors that Chrome and Firefox cover for you).
When running this in IE (I tested this in IE 10), you need to allow blocked content in order for the JavaScript to run. Otherwise the JavaScript will not be allowed to run. Luiggi Mendoza suggested the same for IE 9.
It is worth noting that this works fine in Chrome and FireFox without any user confirmation allowing JavaScript to run.
IE doesn't like ".trim()". I tested in IE8 with IE8 compat mode, and your code didn't work because of trim. Though I imagine IE9 is fine (as noted by Luigi's confirmation).
use something like this instead:
strReplace=str.replace(/^\s+/,'').replace(/\s+$/,'');
You could get around it with something like:
function stripWhiteSpace(arg){
if(arg.replace(/^\s+/,'').replace(/\s+$/,'') == ""){
return true;
}
}
then just call to it and pass your param
if (stripWhiteSpace(b))
I support #rhughes's answer. But If you test this code in IE8(I was using this and your code was not working) and below, The HTML trim() wont work. As you have trim() in 3 places, you have to use the following.
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
or you can more easly use jQuery trim()
$.trim()
refer 1
refer 2
I tried to send a string from an html page (with javascript) to a swf file (action script 2).
i searched in google, found this page.
but the example code (version 1, not 2, you can find it in the source file .zip) didn't work in IE (IE said: object doesn't support this property or method)
where is the problem? (i don't want to use SWFObject.)
the action script :::
//From Evan Mullins # circlecube.com
//View post at http://blog.circlecube.com/2008/02/01/actionscript-javascript-communication/
import flash.external.*;
//Set up Javascript to Actioscript
var methodName:String = "sendTextFromHtml";
var instance:Object = null;
var method:Function = recieveTextFromHtml;
var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);
//Actionscript to Javascript
//ExternalInterface.call("recieveTextFromFlash", _root.theText.text);
function recieveTextFromHtml(t) {
_root.theText.text = t;
}
_root.button.onRelease = function() {
ExternalInterface.call("recieveTextFromFlash", _root.theText.text);
_root.theText.text = "";
}
js:::
function recieveTextFromFlash(Txt) {
document.getElementById('htmlText').value = Txt;
}
and the onclick js code:::
getElementById('flash').sendTextFromHtml(htmlText.value); document.getElementById('htmlText').value = ''
Thank you.
give this javascript code a try?
function getFlashMovie(movieName) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}
function addToResults(results) { getFlashMovie("flashdemo").addToResults(results); }