string replace space and / crashes firefox - javascript

I have a JSON that contains values of "some thing" and "some/thing/other" as possible values that I want to write to:
$('li#'+fixRegion).append('<ul class="sub"/>');
I tried doing:
var fixRegion = region.replace(/\s/g,'');
fixRegion = fixRegion.replace(/\//g,'');
but it crashes firefox and I won't even bother to tell you want it does with firebug open. What horribly obvious thing am I doing wrong?

Related

Selenium C# how to get text in IWEbElement

I am trying to fill in a text-field on a website for which the id changes every time the website is opened. To circumvent this, I look up the webelement by it's class name and this seems to work well as I am able to click on it through
currentWebElement.Click();
however, when I try to fill in the edit text box through
currentWebElement.SendKeys("51");
nothing happens (even more so, focus is lost)
I have searched a lot to see what the problem is but have not been able to find a solution so I was wondering if someone here can point me in the right direction. What I have tried so far:
1) I was working in chrome but since this seems to be a common problem with sendKeys I have swithed to ie, alas the problem persisted. I have tried migrating to firefox but did not succeed as I cannot seem to locate the binary path to firefox.exe but this should be addressed in a different question. So the problem occurs both in chrome and IE.
2) Using javascript to send the command as I have found this suggestion in other questions that were answered on this website:
driver.ExecuteScript("arguments[0].value = '51'", currentWebElement);
directly but also implementing it as a function did not work:
private static object setValue(this IWebDriver driver, IWebElement element, String value) {
return ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].value = arguments[1]", element, value);
}
// and then calling it in my code:
setValue(driver, currentWebElement, "51");
all implementations did not give me errors nor warnings (I managed to get rid of those during the process), but none of them give the desired result i.e. the box keeps remaining empty which is really starting to bug me!
Try the below code. I have used XPATH to sendKeys in the field(FYI the PRMT_TB part of the id doesn't change, even if the rest part does each time, hence the xpath):
var currentWebElement = driver.FindElement(By.XPath("//input[starts-with(#id,'PRMT_TB')]"));
currentWebElement.SendKeys("51");
OR
In case the above doesn't work, please try the below code too
var currentWebElement = driver.FindElement(By.XPath("//td[#class='clsTextWidgetParseError']/input"));
currentWebElement.SendKeys("51");
By byCss = By.CssSelector(".clsTextWidgetParseError>input");
var element = Driver.FindElement(byCss );
element.Clear();
element.SendKeys(value);
I am assuming you are using wrong selector. If this throws any exception provide me the stack trace please

Issue using JavaScript objects in an iOS 7 UIWebView

I am creating an HTML page to be run inside a UIWebView in iOS 7. When ever I try to read a JavaScript object's property, it breaks. It doesn't crash or anything, just doesn't read the property.
Here is my JS:
var TestObject = {};
var TestObject2 = new Object();
TestObject.title = "This is the first Title";
TestObject2["title"] = "This is the second.";
alert(TestObject.title);
alert(TestObject2.title);
I've tried lowercase object names, passing the property in brackets, and no difference. The alerts read "undefined". This code works in iOS Safari, but not within the UIWebView.
Any ideas on what I'm doing wrong?
I have thousands of problems with dealing with alerts from a uiwebview. If i was you i'd send messages from a JS and catch it with a cocoa selector. try using a library like this one:
https://github.com/tcoulter/jockeyjs
and show the alert from a uialertview.

IE Object doesn't support this property or method

This is probably the beginning of many questions to come.
I have finished building my site and I was using Firefox to view and test the site. I am now IE fixing and am stuck at the first JavaScript error which only IE seems to be throwing a hissy about.
I run the IE 8 JavaScript debugger and get this:
Object doesn't support this property or method app.js, line 1 character 1
Source of app.js (first 5 lines):
var menu = {};
menu.current = "";
menu.first = true;
menu.titleBase = "";
menu.init = function(){...
I have tested the site in a Webkit browser and it works fine in that.
What can I do to fix this? The site is pretty jQuery intensive so i have given up any hope for getting it to work in IE6 but I would appreciate it working in all the others.
UPDATE: I have upload the latest version of my site to http://www.frankychanyau.com
In IE8, your code is causing jQuery to fail on this line
$("title").text(title);
in the menu.updateTitle() function. Doing a bit of research (i.e. searching with Google), it seems that you might have to use document.title with IE.
Your issue is (probably) here:
menu.updateTitle = function(hash){
var title = menu.titleBase + ": " + $(hash).data("title");
$("title").text(title); // IE fails on setting title property
};
I can't be bothered to track down why jQuery's text() method fails here, but it does. In any case, it's much simpler to not use it. There is a title property of the document object that is the content of the document's title element. It isn't marked readonly, so to set its value you can simply assign a new one:
document.title = title;
and IE is happy with that.
It is a good idea to directly access DOM properties wherever possible and not use jQuery's equivalent methods. Property access is less troublesome and (very much) faster, usually with less code.
Well, your line 1 certainly looks straight forward enough. Assuming the error line and number is not erroneous, it makes me think there is a hidden character in the first spot of your js file that is throwing IE for a fit. Try opening the file in another text editor that may support display of normally hidden characters. Sometimes copying/pasting the source into a super-basic text-editor, like Notepad, can sometimes strip out non-displayable characters and then save it back into place directly from Notepad.

Creating an export function with JavaScript?

I'm trying to set up an export function in JavaScript for a packaged web app that turns a string stored in localStorage into a plain text file for downloading. As JavaScript does not have access to the computer's file-system, I'd like to set it up so that it create a blank text file (or, failing that, a simple HTML page) and open in in the web-browser; as it wouldn't be accessing any file-systems I was hoping this would be possible.
I was thinking of using a Data URI scheme to open the localStorage as plain text, such as the following:
function exportFile() {
window.open("data:text/plain;charset=utf-8," + localStorage.WebAppData);
};
But it's much slower than I expected, which I guess is because it's sticking the whole document in the URL box. Though probably not an issue with the code, some web browsers, like Google Chrome, won't let me save the resulting file. (And for some reason all the line-breaks have turned into spaces....)
Any suggestions to fix these problems or better ways of doing a similar function will be greatly appreciated!
Did you try something like:
window.open("data:text/plain;charset=utf-8," + localStorage.WebAppData);
For the download, I guess you need a round trip to a server, that will set a mime/type that will make the download box to pop up.
EDIT:
If you use localStorage, may be window.postMessage is available in your environment and could help for speed.
In order to retain line-breaks in the data exported with window.open you may wrap up your data with encodeURI:
var data1 = "Line \n break. And \r\n another one";
window.open("data:application/octet-stream, " + encodeURI(data1));
Otherwise you may export your data encoded in base64 with the btoa function:
var data1 = "Line \n break. And \r\n another one";
window.open("data:application/octet-stream;base64, " + btoa(data1));
Not really a solution, rather a work-around, but your question and the answer by #Mic lead me down this route:
Just use data:text/html as then you can put in line breaks using <br />
I tried everything else (all combinations of unicode characters, etc, ) to get line breaks in text/plain but couldn't get them to show up. document.write() and document.body.textContent(), etc also suffer from the same problem. Line breaks just get ignored.
Since Chrome won't let you save the popup window anyway, the only way to get text out of it is copy and paste so there is no benefit of using text/plain over text/html
In web browsers that will let you save the page (Firefox) you can choose to save the page as text, rather than HTML and so you still get the same end result.
EDIT: This approach works in Chrome, but not Firefox
win = window.open("", "win")
win.document.body.innerText = "Line \n breaks?"
Have to use innerText though. InnerHTML or textContent remove the line breaks. This works on both:
win = window.open("", "win")
win.document.body.innerHTML = "<pre>Line \n breaks?</pre>"
So perhaps you could just wrap everything in <pre> tags? Although I guess both of these have the same "problem" as the ` suggestion in that it's actually creating a HTML document rather than a text/plain one.

IE won't split string with Javascript

I have the following code:
$('#smallcart .plusone').live('click',function(){
var id = $(this).attr('id');
articlenr = id.split('_')[1];
});
this works fine in FF, safari, Chrome, however in IE (7 and 8) it throws an error on the split-function (this property or method is not supported by this object).
if I alert the 'id'-variable I get something like plus_5751. (so I want to get the '5751' part)
if I do alert(typeof(id)) I get String as an answer...
Maybe somebody can point me to the right answer?
Thx
The split works pretty well in IE. The problem is the part left of the equal-sign. It's an object with all input-fields having the name articlenr and therefor IE quits with 'this property or method is not supported by this object' when you're trying to assign a string to it.
Your code works just fine for me in Internet Explorer - as it should be expected to. The problem must lie elsewhere, perhaps something is overriding String.prototype.split?. You can see a working example of your code at http://jsfiddle.net/AndyE/6K77Y/. The first thing to check for is any Internet Explorer specific code in your scripts.
I would make one small improvement for efficiency. $(this).attr('id'); is pretty much the long winded way of writing this.id. It's slower, because a new jQuery object has to be created and then the attr function has to run. Without it, your code can be compressed more, whilst still remaining very readable, if you like:
$('#smallcart .plusone').live('click',function(){
articlenr = this.id.split('_')[1];
});
Try renaming your variable 'id' to something else. IE doesn't like it when you name things in your scripts the same as items in the DOM.
Never mind, that seems to have not been the issue in this case. I have, however, had issues in the past with IE specific bugs caused by variable names.

Categories

Resources