This is a bit embarrassing scenario but I need solution.
I am using some names as an ID for li HTML element. These names are having special characters in it. using JQuery to grab an Id.
lets say my id usage is
var abc = li[id="someCompany=\"Some Term\""]
if I am calling this as
alert($(abc).parent()[0]); or
alert($(abc).html());
I am getting data and everything is working fine in firefox but not in IE.
My actual id display in application is
<li id="someCompany="Some Term""> xyz </li>
I am replacing it with .replace(/"/gi, "\"")
Please let me know, How to get it work in IE, Thanks in advance
Sorry guys, I need to give more information in my question...
Firstly, I get ID as
var aaa = "someCompany="some Data"";
I am replacing it as
aaa = aaa.replace(/"/gi, "\"").replace(/'/gi, "\'").replace(/&/gi, "\&");
Then,
var abc = aaa.replace(/"/gi, "\\\"").replace(/\:/gi, "\:").replace(/'/gi,"\\\'");
and then my question follows....
When I tried changing the above line to
var abc = aaa.replace(/"/gi, "\"").replace(/\:/gi, "\:").replace(/'/gi,"\'");
everything is fine but I have some names like
var aaa = "someCompany="some's Data""; //In this case I need to handle more special chanracters
Here, .replace(/"/gi, "\\\"") is working fine in Firefox but not in IE. How can I make it work in IE as well, please suggest
try using browser validation, like single quotes and slash in IE and double in FF.
Which version of IE are you using? I'm on IE9, and on jsFiddle, that works in IE for me (in IE7/8/9 standards mode as well). The only difference I have between your code and mine is that I put quotes around abc, i.e. var abc = 'li[id="someCompany=\"Some Term\""]' ... I'd assume you already had that there though?
http://jsfiddle.net/benno_007/mhHJh/2/
Although .parent()[0] returns an object, not xyz. If you're just needing to access xyz, just use $(abc).html()
Edit:
An alternative to using those awkward IDs could be:
<li id="someCompany" term="Some Term">test</li>
$('#someCompany[term="Some Term"]').html();
Here: http://jsfiddle.net/benno_007/mhHJh/3/
var abc = $("[id='someCompany=\"Some Term\"']");
alert($(abc).html()); //returns xyz
works for me in IE7 and 8, only difference is the single quotes around the whole attribute value instead of the doubles you have
Related
Or how would I convert a list of SelectItems to a JavaScript array?
Currently I am trying this:
<h:outputScript>
<!-- Trailing commas valid as per http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5 -->
var possibleOption = [<ui:repeat value="#{bean.mySelectItems}" var="selectItem">"#{selectItem.value}",</ui:repeat>];
var firstOption = possibleOption[0];
</h:outputScript>
And it works, except that firstOption is undefined although possibleOption gets correctly populated when I check in the console. Maybe a timing problem? Is this even valid JSF, and if so, is there a "blocking" version of ui:repeat or something?
Or which other approach would you recommend?
Aaaah, I got it:
actually I was using:
var chosenOption = '#{empty bean.chosenOption ? possibleOption[0] : bean.chosenOption}';
Which is (now) of course wrong, because I was using possibleOption[0] inside the EL expression headbang
Sorry, one should always post the actual code I guess, not some dumbed down showcase ;)
I am using replace function to replace profile path of a user globally.
The user.profile_path has value like http://mysite/user/profile/XXXXXX
The code looks like:
.replace(/{:profile_path}/g,user.profile_path)
It is working fine in IE and chrome and latest Firefox browsers.But when i tried the same in Firefox version 14, the replace is not working. I searched and found that giving "g" for replace is not web standard as not all browsers support this. Then i tried giving Regular Expression for same like:
var regProfPath = new RegExp("{:profile_path}","g");
.replace(regProfPath,user.profile_path)
This also didn't replace the profile path globally.
Any help will be appreciated.
Not sure how globally you want to replace this; but assuming for a moment that your HTML content on the page where you want to replace this is in a DIV with class .content, you could use:
var user = {},
content = $('.content').html();
user.profile_path = "http:\/\/mysite\/user\/profile\/XXXXXX";
$('.content').html(content.replace(/\{\:profile_path\}/gm, user.profile_path));
Tested specifically in FF 14 and it worked. Looks like it was likely the escaping of the }s . Even though I added the m (multiline) modifier, I don't think that has an effect; just put it in since I figured you were probably dealing with a large, multiline string if you were replacing globally across stringified HTML.
I have the following javascript which works fine for the most part. It gets the user that has logged in to the site and returns their DOMAIN\username info. The problem arises when the username starts with a letter that completes a valid escape character (eg. DOMAIN\fname). The \f gets interpolated and all kinds of terrible things happen. I have tried every sort of workaround to try and replace and/or escape/encode the '\'. The problem is the \f does not seem like it is available to process/match against. The string that gets operated on is 'DOMAINname'
// DOMAIN\myusername - this works fine
// DOMAIN\fusername - fails
var userName='<%= Request.ServerVariables("LOGON_USER")%>';
userName = userName.replace('DOMAIN','');
alert("Username: " + userName);
I also see all kinds of weird behaviour if I try to do a workaround using the userName variable, I think this may be because it contains a hidden \f. I've searched high and low for a solution, can't find a thing. Tried to find out if I could remove the DOMAIN\ on the serverside but that doesn't seem available either. Does anyone have a solution or workaround? In the debugger, the initial value of the servervariable is correct but the next immediate call to that variable is wrong. So the interpolated values in the debugger look like this:
var userName='DOMAIN\fusername';
userName; // 'DOMAINusername' in debugger.
Thanks
If you're using ASP.net (as it looks like you are), use AjaxHelper.JavaScriptStringEncode or HttpUtility.JavaScriptStringEncode to output the string correctly.
var userName='<%= HttpUtility.JavaScriptStringEncode(Request.ServerVariables("LOGON_USER"))%>';
This might be a noob question, but I have tried to find an answere here and on other sites and I have still not find the answere. At least not so that I understand enough to fix the problem.
This is used in a userscript for chrome.
I'm trying to select a date from a string. The string is the innerHTML from a tag that I have managed to select. The html structure, and also the string, is something like this: (the div is the selected tag so everything within is the content of the string)
<div id="the_selected_tag">
link
" 2011-02-18 23:02"
thing
</div>
If you have a solution that helps me select the date without this fuzz, it would also be great.
The javascript:
var pattern = /\"\s[\d\s:-]*\"/i;
var tag = document.querySelector('div.the_selected_tag');
var date_str = tag.innerHTML.match(pattern)[0]
When I use this script as ordinary javascript on a html document to test it, it works perfectly, but when I install it as a userscript in chrome, it doesn't find the pattern.
I can't figure out how to get around this problem.
Dump innerHTML into console. If it looks fine then start building regexp from more generic (/\d+/) to more specific ones and output everything into a console. There is a bunch of different quote characters in different encodings, many different types of dashes.
[\d\s:-]* is not a very good choice because it would match " 1", " ". I would rather write something as specific as possible:
/" \d{4}-\d{2}-\d{2} \d{2}:\d{2}"/
(Also document.querySelector('div.the_selected_tag') would return null on your sample but you probably wanted to write class instead of id)
It's much more likely that tag.innerHTML doesn't contain what you think it contains.
While debugging some jQuery that is not working in IE, I found this error message:
var item = $("#item_"+ itemArray[itemIndex]).find('a').text().trim();
Object doesn't support this property or method (script.js, line 100, character 2)
The character 2 doesn't make sense to me. Based on the text displayed character 2 would be the letter a in var but of course that doesn't make any sense.
(Should I not use var?)
I know that jQuery is working to some extent or the script would not have been able to get this far on my page.
IE doesn't have String.trim(), you'll need $.trim() (which uses native trim if available, emulates it in IE), like this:
var item = $.trim($("#item_"+ itemArray[itemIndex]).find('a').text());
IE doesn't have a trim method.
Instead, you can call jQuery.trim(...).