Discussion:
Custom event listeners
jonbutler88
2008-11-14 04:44:50 UTC
Permalink
I have an iframe on a page, and im trying to write some ajax to check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I have
to assign it with javascript. Im still pretty new to mochikit, and im
wondering what the best way to set this up would be?

Thanks in advance, sorry if its a bit vague.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
Yoann Aubineau
2008-11-14 10:21:25 UTC
Permalink
Assuming your iframe's id is "foo", you would do :

connect("foo", "onchange", function(e) {
logDebug($("foo").src);
});

Not sure what "onchange" event means on an iframe though.

See http://mochikit.com/doc/html/MochiKit/Signal.html#fn-connect for
more information about signals.

-- Yoann
Post by jonbutler88
I have an iframe on a page, and im trying to write some ajax to check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I have
to assign it with javascript. Im still pretty new to mochikit, and im
wondering what the best way to set this up would be?
Thanks in advance, sorry if its a bit vague.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
jonbutler88
2008-11-16 06:33:07 UTC
Permalink
Yeah, turns out onchange doesnt detect the change in src.... Im gonna
have to write my own event by the looks of it, something that baffles
me a fair bit. Ive seen lots of tutorials using prototype, but none
that focus around mochikit.... Ive had a good look at the
mochikit.signal docs, but im no closer to understanding how to write
my event. Im clearer about how to attach the event and fire it, but im
lost with how to define when it fires...

Thanks very much for your help so far :)
Post by Yoann Aubineau
connect("foo", "onchange", function(e) {
    logDebug($("foo").src);
});
Not sure what "onchange" event means on an iframe though.
Seehttp://mochikit.com/doc/html/MochiKit/Signal.html#fn-connectfor
more information about signals.
-- Yoann
Post by jonbutler88
I have an iframe on a page, and im trying to write some ajax to check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I have
to assign it with javascript. Im still pretty new to mochikit, and im
wondering what the best way to set this up would be?
Thanks in advance, sorry if its a bit vague.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
Yoann Aubineau
2008-11-16 10:06:27 UTC
Permalink
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src....
Maybe "onload" will do. I never use iframes so I can't say for sure it
will work on every browser. But it sounds like a logical try to me.

If the iframe's source changes on a link or button click, you can also
listen for those events, check if the iframe's source is actually
different from the previous value and then fire the custom event you
need.

Otherwise, setup a loop that check regulary iframe.src but … eark !

-- Yoann
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src.... Im gonna
have to write my own event by the looks of it, something that baffles
me a fair bit. Ive seen lots of tutorials using prototype, but none
that focus around mochikit.... Ive had a good look at the
mochikit.signal docs, but im no closer to understanding how to write
my event. Im clearer about how to attach the event and fire it, but im
lost with how to define when it fires...
Thanks very much for your help so far :)
Post by Yoann Aubineau
connect("foo", "onchange", function(e) {
logDebug($("foo").src);
});
Not sure what "onchange" event means on an iframe though.
Seehttp://mochikit.com/doc/html/MochiKit/Signal.html#fn-connectfor
more information about signals.
-- Yoann
Post by jonbutler88
I have an iframe on a page, and im trying to write some ajax to check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I have
to assign it with javascript. Im still pretty new to mochikit, and im
wondering what the best way to set this up would be?
Thanks in advance, sorry if its a bit vague.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~
Jon Butler
2008-11-16 14:25:00 UTC
Permalink
Yeah, that was my original thinking, but onload doesnt fire when I
change the src.

I tried using a loop before, but I thought it would be more efficient
to use a custom event. Thiinking about it, the listener would still
need to check when the src changes so it's pretty much the same
solution.

Il have another go and post back if I run into problems.

Cheers
Post by Yoann Aubineau
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src....
Maybe "onload" will do. I never use iframes so I can't say for sure it
will work on every browser. But it sounds like a logical try to me.
If the iframe's source changes on a link or button click, you can also
listen for those events, check if the iframe's source is actually
different from the previous value and then fire the custom event you
need.
Otherwise, setup a loop that check regulary iframe.src but … eark !
-- Yoann
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src.... Im gonna
have to write my own event by the looks of it, something that baffles
me a fair bit. Ive seen lots of tutorials using prototype, but none
that focus around mochikit.... Ive had a good look at the
mochikit.signal docs, but im no closer to understanding how to write
my event. Im clearer about how to attach the event and fire it, but im
lost with how to define when it fires...
Thanks very much for your help so far :)
Post by Yoann Aubineau
connect("foo", "onchange", function(e) {
logDebug($("foo").src);
});
Not sure what "onchange" event means on an iframe though.
Seehttp://mochikit.com/doc/html/MochiKit/Signal.html#fn-connectfor
more information about signals.
-- Yoann
Post by jonbutler88
I have an iframe on a page, and im trying to write some ajax to check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I have
to assign it with javascript. Im still pretty new to mochikit, and im
wondering what the best way to set this up would be?
Thanks in advance, sorry if its a bit vague.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
jonbutler88
2008-11-16 23:52:57 UTC
Permalink
Ok, so I have this code:

[code]

function check(tab) {
var src = $('editor').src;
if (src.substring(0,31) != "<my site.com>")
{
alert("access violation");
removeElement(tab);
focusTab('ide_tab0', 'start.php');
return false;
}
else
{
setTimeout(check(tab), 1000);
return true;
}
}

[/code]

But this makes an infinite loop, and the function eventually fails due
to recursion errors. Is there any way to have this run in the
background? So other code can run while it is waiting for execution? I
have it setup with connect('editor','onload',check(tab)), so the DOM
should load before it runs, and there should be no errors, it just
needs to be able to load the rest of the page while the loop is
running.

Any thoughts?

Thanks
Yeah, that was my original thinking, but onload doesnt fire when I  
change the src.
I tried using a loop before, but I thought it would be more efficient  
to use a custom event. Thiinking about it, the listener would still  
need to check when the src changes so it's pretty much the same  
solution.
Il have another go and post back if I run into problems.
Cheers
Post by Yoann Aubineau
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src....
Maybe "onload" will do. I never use iframes so I can't say for sure it
will work on every browser. But it sounds like a logical try to me.
If the iframe's source changes on a link or button click, you can also
listen for those events, check if the iframe's source is actually
different from the previous value and then fire the custom event you
need.
Otherwise, setup a loop that check regulary iframe.src but … eark !
-- Yoann
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src.... Im gonna
have to write my own event by the looks of it, something that baffles
me a fair bit. Ive seen lots of tutorials using prototype, but none
that focus around mochikit.... Ive had a good look at the
mochikit.signal docs, but im no closer to understanding how to write
my event. Im clearer about how to attach the event and fire it, but  
im
lost with how to define when it fires...
Thanks very much for your help so far :)
Post by Yoann Aubineau
connect("foo", "onchange", function(e) {
   logDebug($("foo").src);
});
Not sure what "onchange" event means on an iframe though.
Seehttp://mochikit.com/doc/html/MochiKit/Signal.html#fn-connectfor
more information about signals.
-- Yoann
I have an iframe on a page, and im trying to write some ajax to  
check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I  
have
to assign it with javascript. Im still pretty new to mochikit,  
and im
wondering what the best way to set this up would be?
Thanks in advance, sorry if its a bit vague.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
jonbutler88
2008-11-17 05:30:39 UTC
Permalink
Ok, so ive run into a brick wall with this loop. Here is what I have:

function init(tab) {
connect('editor','onload',function () {
if (document.getElementById
('editor').contentWindow.document.designMode != "on")
{
document.getElementById
('editor').contentWindow.document.designMode = "on";
}
document.getElementById('editor').contentWindow.focus();
setInterval("check("+tab+")",5000);
});
}

function check(tab) {
var src = document.getElementById('editor').src;
if (src.substring(0,31) != "<my site.com>")
{
alert("access violation");
removeElement(tab);
focusTab('ide_tab0', 'start.php');
}
}

But im running into problems with the loop. While it is running, the
rest of the page stops loading. Is there any way I can get the loop to
run in the background?

Thanks
Yeah, that was my original thinking, but onload doesnt fire when I  
change the src.
I tried using a loop before, but I thought it would be more efficient  
to use a custom event. Thiinking about it, the listener would still  
need to check when the src changes so it's pretty much the same  
solution.
Il have another go and post back if I run into problems.
Cheers
Post by Yoann Aubineau
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src....
Maybe "onload" will do. I never use iframes so I can't say for sure it
will work on every browser. But it sounds like a logical try to me.
If the iframe's source changes on a link or button click, you can also
listen for those events, check if the iframe's source is actually
different from the previous value and then fire the custom event you
need.
Otherwise, setup a loop that check regulary iframe.src but … eark !
-- Yoann
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src.... Im gonna
have to write my own event by the looks of it, something that baffles
me a fair bit. Ive seen lots of tutorials using prototype, but none
that focus around mochikit.... Ive had a good look at the
mochikit.signal docs, but im no closer to understanding how to write
my event. Im clearer about how to attach the event and fire it, but  
im
lost with how to define when it fires...
Thanks very much for your help so far :)
Post by Yoann Aubineau
connect("foo", "onchange", function(e) {
   logDebug($("foo").src);
});
Not sure what "onchange" event means on an iframe though.
Seehttp://mochikit.com/doc/html/MochiKit/Signal.html#fn-connectfor
more information about signals.
-- Yoann
I have an iframe on a page, and im trying to write some ajax to  
check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I  
have
to assign it with javascript. Im still pretty new to mochikit,  
and im
wondering what the best way to set this up would be?
Thanks in advance, sorry if its a bit vague.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
Per Cederberg
2008-11-17 08:18:58 UTC
Permalink
When you call setInterval and setTimeout you should provide functions.

In your first example you perform a function call to setTimeout and
then send the result to setTimeout. That leads to an infinite loop.
Instead, do this:

var func = function() { check(tab); };
setTimeout(func, 1000);

In your second example you construct a string which the JS interpreter
does "new Function(...)" with. That is inefficient. And your 'tab'
argument will also be converted into a string (with toString) which is
probably not correct. Instead, do this:

var func = function() { check(tab); };
setInterval(func, 5000);

Or if you want to do it "the MochiKit way":

setInterval(bind(check, this, tab), 5000);

Cheers,

/Per
Post by jonbutler88
function init(tab) {
connect('editor','onload',function () {
if (document.getElementById
('editor').contentWindow.document.designMode != "on")
{
document.getElementById
('editor').contentWindow.document.designMode = "on";
}
document.getElementById('editor').contentWindow.focus();
setInterval("check("+tab+")",5000);
});
}
function check(tab) {
var src = document.getElementById('editor').src;
if (src.substring(0,31) != "<my site.com>")
{
alert("access violation");
removeElement(tab);
focusTab('ide_tab0', 'start.php');
}
}
But im running into problems with the loop. While it is running, the
rest of the page stops loading. Is there any way I can get the loop to
run in the background?
Thanks
Post by Jon Butler
Yeah, that was my original thinking, but onload doesnt fire when I
change the src.
I tried using a loop before, but I thought it would be more efficient
to use a custom event. Thiinking about it, the listener would still
need to check when the src changes so it's pretty much the same
solution.
Il have another go and post back if I run into problems.
Cheers
Post by Yoann Aubineau
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src....
Maybe "onload" will do. I never use iframes so I can't say for sure it
will work on every browser. But it sounds like a logical try to me.
If the iframe's source changes on a link or button click, you can also
listen for those events, check if the iframe's source is actually
different from the previous value and then fire the custom event you
need.
Otherwise, setup a loop that check regulary iframe.src but … eark !
-- Yoann
Post by jonbutler88
Yeah, turns out onchange doesnt detect the change in src.... Im gonna
have to write my own event by the looks of it, something that baffles
me a fair bit. Ive seen lots of tutorials using prototype, but none
that focus around mochikit.... Ive had a good look at the
mochikit.signal docs, but im no closer to understanding how to write
my event. Im clearer about how to attach the event and fire it, but im
lost with how to define when it fires...
Thanks very much for your help so far :)
Post by Yoann Aubineau
connect("foo", "onchange", function(e) {
logDebug($("foo").src);
});
Not sure what "onchange" event means on an iframe though.
Seehttp://mochikit.com/doc/html/MochiKit/Signal.html#fn-connectfor
more information about signals.
-- Yoann
Post by jonbutler88
I have an iframe on a page, and im trying to write some ajax to check
the src variable when onchange is called. However, it has to be
secure, so rather than include onchange='func()' in the HTML, I have
to assign it with javascript. Im still pretty new to mochikit, and im
wondering what the best way to set this up would be?
Thanks in advance, sorry if its a bit vague.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---

Continue reading on narkive:
Loading...