javascript and email - javascript

I get it to send an email using javascript I use window.open('mailto:xxx') What I would like to avoid is opening a new browser window + opening an email window. Works fine except for that. I do my programming in CF. Is this possible?

Don't use "window.open()"
Either use an in your HTML,
or use location.href="mailto:XXX"; if it needs to be Javascript.

location.href="mailto://xxx";

Use location.assign():
location.assign('mailto:user#example.com');
While you can instead directly assign to location's properties (location.href='mailto:...';) to cause the browser to navigate, I recommend against this.
Internally, doing so is just calling location.assign() anyway, and assigning to properties does not always behave the same in all browsers.

Related

Hook window.location.* assignments and window.location.assign

I'm trying to intercept window.location.* assignments and window.location.assign calls to change the url assigned before leaving is that possible?
when I try to redefine the property setter I get an error that I can't redefine it.
Is my only option is to proxy the page and statically replace all assignments to window.location with string replace?
Although I rather avoid it since javascript is funky and something like this could also be valid so I would have to keep track of all assignments:
var l = window.location;
var c = l;
var t = c.assign;
t('...');
One solution would to be to create your own location change provider where you could intercept the URL and make changes accordingly. Your code could then always call your change provider rather than the standard window.location property.
Of coarse this would not work if you are in a situation where the code that is setting the location property is out of your control.
If the location setting code is out of your control, take a look at Javascript: How to intercept window.location change where the beforeunload event is used.
Unfortunately, I don't think you can do this. As you've found, the location property on window is non-configurable (at least in Chrome and Firefox). And as you know, it's very special: If you attempt to write to it (e.g., to replace it with your own customized object), instead of replacing the property in the normal way, it will convert what you give it to a string and attempt to navigate there. Consequently, there's no way for you to replace window.location with your own thing: Object.defineProperty won't let you (because it's non-configurable), and assignment won't work.
That leaves you with the task of identifying all writes to window.location in the JavaScript code on the page, which is impossible in the general case. While you could find all window.location and location references, and static analysis would tell you (absent eval or new Function) whether those window and location variables are the globals, you would need to evaluate the code step-by-step to find the kind of thing you mentioned in your question, or even something simple like:
(function(w) {
w.location = "https://stackoverflow.com";
})(this);
Completely changing the architecture of your solution, you could run a headless browser server-side and echo changes to its DOM to the client, intercepting all clicks and forwarding them to server-side code to pass to the headless browser. Which likely has its own significant challenges.

How to identify which IFRAME is which?

How to refer to a component inside an IFRAME is showed in this reply. It works under the assumption that there's only one IFRAME (or that we are certain that the zero-th will suffice). However, I wonder how to identify the actual IFRAME I'd like to poke.
I need the syntax for changing:
window.frames[0].document. + my stuff
into something like:
window.frames["theIdOfMyIFrame"].document. + my stuff
but I can't get it right.
I dealt with this problem earlier.
If you show the response of a cross-site request in an iframe, the new browsers are denying the access to this iframe, because of the same-origin policy or cross-site scripting/request prohibition.
For more information see here: IFrame Permission Denied
If you want to dynamically add content, this solution might help you: Add Dynamic Content
Best regards
I would avoid using window.frames. (Might be due to the lack of experience with them.)
When a reply on the net is on form something[0], be cautious. Bad coding style, probably.
The reference to your theIdOfMyIFrame should be used.
The inner document is right under contentDocument.
You can refer to the embedded components like this.
document.getElementById("theIdOfMyIFrame")
.contentDocument.getElementById("controlIdInTheembeddedPage")
.value = "Christophe has eyes for details!";
Use document.getElementById instead of the nasty deprecated window.frames property:
document.getElementById("theIdOfMyIFrame").contentDocument ...

How to access JSON from an iframe originating from same domain?

In my webpage a hidden iframe is loaded with some JSON in it. This JSON is refreshed by some actions on the page. How do I access this JSON in iframe from my web page? for some unknown arcane unexplainable reason I am forced to use jQuery 1.3.2. so no $.parseJSON()
I think you can use:
var json = $.parseJSON($("#hiddeniframe").contents().text());
Something along those lines will work at least.
All modern browsers include a JSON parsing library:
var data = JSON.parse($("#hiddeniframe").contents().text());
If you need to support older browsers there are several libraries to choose from that will provide the same interface. The better ones will check to see if the browser is providing a native implementation and not override it, since it's bound to be faster.
See also JSON.stringify()
The code #Paulpro posted:
var json = $.parseJSON($("#hiddeniframe").contents().text());
doesn't work for me.
I changed the code to:
var json = $.parseJSON($("#hiddeniframe").contents().find("*").first().text());
And now it works.

JavaScript: get and set variables in window.location.hash?

Does anyone have a good solution for getting and setting variables in window.location.hash?
Take a URL that looks like this:
domain.com/#q=1&s=2
What I'd like is an unstressful way - JavaScript or jQuery - to check the values of q and s when the page loads, and change them following events on the page.
I have found some code for getting hash variables, but nothing sensible for setting them.
Am I missing something really obvious, or do I need to roll my own solution (and release it!)?
Thanks.
Haven't used it but there is jHash
jHash allows you to work with the
'location.hash' value in a similar
fashion to a server-side query string.
This library utilizes the HTML5
"onhashchange" event, but also
includes a fall back to still allow
the change notifications to work
properly in older web browsers.
jQuery BBQ can do this.
See also:
Get URL parameter with jQuery
Get QueryString values with jQuery
Edit as #gonchuki points out, jQuery.query can also do this.
JHash didn't work for me in that I wanted it to trigger the routes right away. I personally used routie instead.
It lets you do advanced routing just like jHash but will trigger on page load correctly.
Below will match example.com/#users/john
routie('users/:name', function(name) {
//name == 'bob';
});

Is there a good way to prevent jQuery from being redefined?

I encountered a problem that took me some time to debug where a plug-in that I was using for jQuery (in this case jFeed) was not working. The problem ended up being because we also used Amazon Associates product previews. The product previews code ends up including a number of other JS files through document.write(), including another copy of jQuery. Because the product previews code appeared below the jFeed script, jQuery was redefined without the getFeed function.
Is there a best practice to ensure that certain objects like jQuery only get defined once on a page? I'm thinking of something like #ifndef with C/C++, but I don't know how it would work in this case where I didn't write the code that dynamically pulled in jQuery again.
I think in your situation, it would probably be best to redefine the jQuery variable as something else. The other jQuery code might use a different version so you might want to define a new variable which would indicate which jQuery you're using.
You could so something like this:
<script>
var $jMain = jQuery;
</script>
You would then just use the $jMain instead of jQuery or $. It'll be up to you to you to ensure you have the correct jQuery object when you do this. Here's the documentation.
Unfortunately the environment inside one JS sandbox (like within a window or frame of a browser) was not really designed to support the modern world of pulling in scripts from various places; there's no way you can say "define this object and make it resistant to redefinition". (You can even redefine most of the Javascript built-ins if you try!)
Your best shot is to make sure that your code is eval'd last, which gives you final say over the state of the environment when it runs. That doesn't mean other code can't come along later and clobber your definitions, but that's generally really bad form. You can do this by having your script tag be the last element in the body of the document, for example.
See also this jQuery method, which won't help you directly, but gets you thinking about some solutions to page sharing: http://api.jquery.com/jQuery.noConflict/

Categories

Resources