Anchor tag href attribute incorrect in IE11 during ace editor change event - html

See the following fiddle: http://jsfiddle.net/R3VbC/4/
In the example fiddle, I run the parseHref function when the ace editor's value changes. This in turn passes a string to an anchor element which is used to normalise the url. You can also click the Parse href button to run this function directly.
var parseHref = function()
var anchorNodeHrefOriginal = urlParsingNode.href;
urlParsingNode.setAttribute("href", 'my-href')
var firstPassHref = urlParsingNode.href;
urlParsingNode.setAttribute("href", firstPassHref)
var finalHref = urlParsingNode.href;
$('#original-href').html(anchorNodeHrefOriginal);
$('#first-pass-href').html(firstPassHref);
$('#final-href').html(finalHref);
}
editor.getSession().on('change', parseHref)
This replicates the functionality used by angularJS to resolve url (see this). This is relevant as I am trying to embed the ace editor in an angular app.
To reproduce the issue I am experiencing, copy and paste some text (do not just type it!) into the editor. In chrome, the values of the text displayed at the bottom should all be something like http://fiddle.jshell.net/R3VbC/4/show/my-href. If you do the same thing in IE11, the values displayed are simply 'my-href'.
If run this parseHref function in any other way, such as typing into the editor or clicking the parse href button, you get the desired result (i.e. http://fiddle.jshell.net/R3VbC/4/show/my-href). It only seems to give the incorrect value if you copy and paste text into the editor.
This could also be worked around by executing the parseHref function from window.setTimeout (see http://jsfiddle.net/R3VbC/5/)
editor.getSession().on('change', function() {
window.setTimeout(parseHref);
})
So it appears that somehow the ace editor is preventing anchor tags from resolving correctly in IE11 during the change event. Why is this happening and can it be prevented?

This ie11 error which happens with textarea too http://jsfiddle.net/R3VbC/6/.
urlParsingNode.setAttribute("href", firstPassHref) doesn't work when called from paste event.
try
textarea.addEventListener("paste", function(){
parseHref()
})
// not very a useful code snippet, but without it
// this `extremely intelligent` site removes jsfiddle link:(
But more importantly you shouldn't do any slow operations, like accessing dom, from change event, use editor 'input' event which is fired with ~30ms timeout, or use your own longer timeout.

Related

Is there any way we can directly connect to the function without wanting to click the button i.e., when ever the page loads the function gets called

here is the Html button i want to disable the click function, reducing the extra step so that the function gets called whenever the page gets loded in the browser.
i want to scrape the data(info that comes after clicking the button) from this html file.
OR
Suggest me if we can dirctly call this JS function In Python
I tried Js2Py but cannot translate the Js File.
i have not tried anything as im not fimailar with html.
i'm supposed to modify the code according to my preferences and im a novoice in html.
i searched in google but couldnt find any relaiable source.
the answer is onload function on the Body tag of html instead for onclick it took a while to figure out.
before:
After:
.
.

Dynamic html onClick

So I am playing around with the HTML service of GAS.
It has been working fine (more or less). But I hit a wall now.
I want the script (inside the html file) to create some html on when called.
In particular a few tags.
However, I want those tags to have onClick handlers (which will edit the div element).
Now the problem is that the onClick should depend on certain properties and I can not
pre-create those objects, they have to be made pragmatically and for some reason when I add a
onClick="someFunction(elementID)" after the new code has been added to the old one the click handler disappears.
(it works tho if the handler function has no parameters)
eg.
var div="<div id=\"box"+count+"\" class=\"square\" insert></div>";
if(something)
div=div.replace("insert", "onclick=\"myFunction(box"+count+"\"");
This is intentional and documented: see the section called "Dynamically adding scripts or external CSS" in the HtmlService user guide.

How to get the changed content in the page?

I wrote a user script for the latest Chrome browser. It seems the script can't get the changed content of the page after loaded (for example, after the page loaded, I clicked ¿sth? and an embedded window popped up).
Even if I used window.setTimeout(), I still can't the get updated content in the timer callback through document.getElementById(). I inspected the page and found that the popup element existed in the DOM.
Is this a limitation of user script? Or some other methods could be used to get the update in user script?
Update:
I tried DOMSubtreemodified event as suggested. But the behavior is still strange.
I added only one one line of JavaScript to the userscript for my.safaribooksonline.com,
document.addEventListener("DOMSubtreeModified", function () {
alert(eval('typeof OpenSignInPopup')); });
But the alert box shows "undefined" as the evaluate result of OpenSignInPopup. But I can run the alert statement in the script console in the same page at the same time, and shows the result as "function".
This function was not loaded when the user script is running at first. So how can I use it in the user script?
You need to provide details, like the relevant code snippet(s) and the pages targeted.
In general, you can fire off the DOMSubtreeModified event in chrome. That should get you access to the changed DOM.
Also, are you sure the new content is not in an iframe?
Update for new OP info:
But the alert box shows "undefined" as the evaluate result of OpenSignInPopup.
In Chrome, Greasemonkey code cannot interact with the page's JS functions like that. You'll need to inject your code into the page. See this SO answer for more information.

mailto link not working in chrome extension popup

This issue drove me nuts for 2 days. I made a simple chrome extension which calls a server-side program that returns HTML that I then stuff into a div in the popup. It was all fine, except for the simple anchor link containing a "mailto:xxx#yyy.com" href. An email message composition window would not pop up.
Workaround: Add target="_blank" attribute
I would like to know why this is necessary.
It might have something to do with extensions running in separate processors from the browser, and therefore a target attribute is needed so that a new tab/window can be opened... there are some websites that don't work when displayed inside extension popups for this reason, because the extension frame won't navigate to certain pages...
I know this is an old question, but I ran into a similar situation. I had to send an email, but I had to do it with a button instead of a link and had to finagle this:
function sendEmail(){
var mail = 'mailto:xxx#yyy.com?subject=Subject&body=Body';
var newWin = window.open(mail);
setTimeout(function(){newWin.close()}, 100);
}
It's not ideal, because it opens a new window that's visible to the user instead of doing it instantly. In fact, my first attempt was this (which works in an HTML file, but doesn't work in my extension):
function sendEmail(){
var mail = 'mailto:xxx#yyy.com?subject=Subject&body=Body';
window.open(mail).close();
}
Not sure why adding a timer makes it work in this instance as opposed to just doing it like in a regular HTML file, but that worked for me so I thought I'd share.

How do I focus an existing tab in a window? (web page, not extension)

I'm trying to focus an existing tab when the content reloads. The usual window methods don't seem to work.
Here's whats happening: On page_1 I have a link like...
Go to my other page
If the tab doesn't exist, when the link is clicked it opens a new tab and takes focus. (Perfect)
If you then go back to page_1 and click the link again, it reloads the content in the existing tab (perfect) but doesn't focus (crap). I've tried the usual window.focus, $(window).focus methods on load with page_2 without luck.
Any recommendations?
It is impossible.
The following appears to work in IE8 and FF13:
<script type="text/javascript">
// Stupid script to force focus to an existing tab when the link is clicked.
// And yes, we do need to open it twice.
function openHelp(a) {
var tab = window.open(a.href, a.target);
tab.close();
tab = window.open(a.href, a.target);
return false;
}
</script>
Help
There is a workaround to this. Use javascript to open a window in a new tab, store a reference to that tab, and when you want to focus it; close it first and then re-open it.
if (window.existingWindow != null)
try { window.existingWindow.close(); } catch (e) { };
window.existingWindow = window.open("/your/url", "yourTabName");
We use a similar approach to opening the preview pane of the current page you're working on in our service called Handcraft where the above works as expected (we wanted the new window to always focus).
Without using a framework you can put a script block at the bottom of your page that will run once the page loads. Because it is after your HTML you can be assured that the HTML is refers to is actually available.
The script can set the focus to the element you want.