svg trigger animation issue (svgator) - javascript

Can you tell me why this example http://www.silkcutz.co.uk/test/ taken from here https://github.com/SVGator/trigger-animation which uses (logo.svg with ID "e6flsqoxhzzs1") works absolutely fine....
BUT http://www.silkcutz.co.uk/test/2.html (using Untitled.svg with ID "eOKHP9YQpje1") doesnt, this is a very simple animation of a box moving position (just for test purposes).
Both html files are very simple and should require only the filename and ID changing but the examples 2.html... you would think the svg has issues but when testing the SVG file (Untitled.svg) using the upload svg facility on the live demo here https://www.svgator.com/help/getting-started/svgator-player-js-api it work fine and is triggered as expected!
Any help would be very much appreciated!

To use the technique described there, you need to export the SVGator project using "Animation start: On click".
But programmatic animation has been recently implemented. Check these resources:
https://www.svgator.com/help/getting-started/animate-programmatically
https://www.svgator.com/help/getting-started/svgator-player-js-api
Here's a complete example on how to use Player API with a SVG embedded in an tag:
<!DOCTYPE html>
<html>
<head>
<title>Testing SVG</title>
<script>
//Wait for the document to load
document.addEventListener("DOMContentLoaded", function() {
//grab the <object> element
let object1 = document.getElementById('animated-svg');
//Wait for the content of the <object> to load
object1.addEventListener("load", function() {
//grab the <svg> element
let svg = object1.contentDocument.getElementById('e5478wvxh25l1');
//Wait for the SVGtor player to be ready for use
svg.svgatorPlayer.ready(function(player) {
//inset your code here; i.e.
player.play();
});
});
});
</script>
</head>
<body>
<object id="animated-svg" type="image/svg+xml" data="animated.svg" style="height: 857px;"></object>
</body>
</html>
It's also worth knowing that no matter how you set the "Animation start", you always have access to the Player API and can control the animations as a PRO user.

Related

Javascript+HTA, reload iframe and keep scroll position

I created an account a very long time ago but I never really use it, as I always manage to find the answers to my questions in already solved threads. So, this is the first time since I'm working on my current program that I was not able to find a working answer on SO. However, if I simply missed it, please be nice to me ^^
A bit of context that may explain why I can't manage to apply any given solution despite many threads exists about this... I am doing:
A HTA Interface
With an iframe to a local text file
And a link to a js file that I want to use to refresh the iframe every 500 ms.
The goal is to make a chat; people can write on the text file and it automatically appears on the HTA interface.
I don't know ANYTHING about JS, frankly. So I found here that piece of code to refresh the iframe.
window.setInterval(function() {
document.getElementById('chatbox').contentWindow.location.reload();
}, 500);
It works, but upon refreshing the iframe, it scrolls back to the top; which makes the chat unreadable, as you can guess. Many solutions that I've read on SO won't work for me, maybe because it doesn't fit with this way of refreshing iframes, or with HTA, or I'm just too dumb with js to know how to make them work.
If anyone has a solution that I could just copy and use, even if I don't understand what I'm doing - it won't be very satisfying intellectually but at least I could focus on finishing my interface :) thanks a lot!
If you refresh the iframe by updating innerHTML from the text file, instead of doing a reload, it won't change the scroll position. Here's an example HTA:
<!DOCTYPE html>
<meta http-equiv="x-ua-compatible" content="ie=9">
<html>
<head>
<script>
function Refresh() {
var Iframe = document.getElementById("chatbox").contentWindow;
window.setInterval(function() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile(".\\Chat.txt",1);
var FileContents = fh.AtEndOfStream ? "" : fh.ReadAll();
fh.Close();
Iframe.document.body.innerHTML = "<pre>" + FileContents + "</pre>"
}, 500);
}
</script>
<style>
#chatbox {height:30em;}
</style>
</head>
<body onload="Refresh()">
<iframe id=chatbox title="Chat Box">
</iframe>
</body>
</html>

How to run code only when iframe loaded

I have been battling with this issue for a few days now and finally found a partial solution but I think it could be improved.
In my project, I have a series of iframes that contain videos. When a link is clicked are displayed with a slide transition and when the link is clicked again the video stops and the span containing the iframe is hidden also with a transition.
This is achieved by adding and removing a css class of "open" that has a height and a transition in it. In addition to this I have an event listener that collapses the containing span also when the video finishes. All this works fine and to save time I am not posting the code.
The issue I was having was with slow page loading times, so I removed the src attribute for the iframes from the html and moved it to my js file and assigned it only after the click is performed. This wasn't working and I realised I needed the iframe to fully load before running the rest of the code inside the "click" method. So I delayed this part of the code by 100ms.
All this works, but I feel it would be better to have the rest of the code run not after a 100ms lapse but when the iframe is loaded (in case page viewed by slower computers). Not sure how to do this.
Here is the code as it stands now:
var player;
var frame = $("#frame");
frame.bind("load", function () {
player = $(this).contents().find("#myVid");
player.on('ended', function () {
frame.removeClass("open");
});
});
$("#clickableLink").click(function(){
if (frame.hasClass("open")) {
frame.removeClass("open");
frame.contents().find('#myVid').get(0).pause();
} else {
function delayed(){
frame.addClass("open");
frame.contents().find('#myVid').get(0).play();
}
frame.attr("src","iframe.html");
setTimeout(delayed, 100);
}
});
Fairly new to development so I am looking for the simplest way to do this. Any help appreciated.
Here is a super simple example of calling code when the iframe has loaded. Check out the onload attribute of the iframe tag:
<head>
<script>
function frameLoaded() {
alert('frame loaded!');
};
</script>
</head>
<body>
<iframe id="frame" src="https://en.wikipedia.org/wiki/HTML_element#Frames" onload="frameLoaded(this)" />
</body>

Altering CreateElement method via Javascript doesn't work for iframes

I would like to globally redefine a createElement method, but unfortunately it only works in a main document, and is ignored in iframes. Let's take this example code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
old_createElement = document.createElement;
document.createElement = function(el_type) {
new_el = old_createElement.call(this, el_type);
new_el.style.color="red";
return new_el;
};
</script>
</head>
<body bgcolor="white">
<iframe id="iframe1"></iframe>
<script type="text/javascript">
window.setTimeout(function(){
iframe_el = document.getElementById("iframe1").contentDocument.createElement("div");
iframe_el.innerHTML = 'inside iframe';
document.getElementById("iframe1").contentDocument.body.appendChild(iframe_el);
},50);
no_iframe_el=document.createElement('div');
no_iframe_el.innerHTML = 'outside of iframe';
document.body.appendChild(no_iframe_el);
</script>
</body>
</html>
When i open in in a browser, the element created in a main document has red color, as expected, but the one in the iframe is black.
The problem is that I only have control on the script contained in the HEAD section of the document. In other words, i don't know how many iframes there will be later on in the HTML source, or how they will be names, or if they are added via user's Javascript.
My question is: how can i change the method globally, so all elements created in iframes also use this new style?
Thanks a lot!
Each frame has it's own separate Javascript context. If you want to change that frame's context, you have to do it specifically for that frame.
In your specific example, each frame has its own document object so it should be no surprise that each document has its own .createElement property.
You cannot generically change things in a way that will affect all frames. And, in fact if it's a cross-origin frame, you can't change it at all.

Reference loaded <img> in javascript

I use a carousel jQuery plugin which defines a container which must contain <img> tags. The images are loaded and shown, the carousel works.
I am now implementing image preview on hover. I've created a separate <div> for this purpose which is being shown with the loaded image.
The problem is that I'm programatically creating a new <img> tag within the <div> each time the mouse hovers over a different image. This results in massive amount of unnecessary server requests.
How can I use preloaded images from the carousel within the image preview div?
I don't need a fully working solution, I'll accept abstract answers.
Further to the comments, here's a working sample of the cloneNode method I mentioned. I've not bothered with the mechanics of the containers or of triggering the behaviour via a mouse hover, just click the existing image and it will work.
You'll also have to change the image source to something you already have. I'm testing with the full-res version of this image: https://commons.wikimedia.org/wiki/File:SoundingRocketSamplePayload-02.jpg
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
function byId(id,parent){return (parent == undefined ? document : parent).getElementById(id);}
window.addEventListener('load', onDocLoaded, false);
function onDocLoaded()
{
byId('srcImg').addEventListener('click', myTest, false);
}
function myTest()
{
var tgtElem = byId('srcImg');
var clone1 = tgtElem.cloneNode(true);
var clone2 = tgtElem.cloneNode(true);
var clone3 = tgtElem.cloneNode(true);
document.body.appendChild(clone1);
document.body.appendChild(clone2);
document.body.appendChild(clone3);
}
</script>
<style>
</style>
</head>
<body>
<img id='srcImg' style='width: 256px' src='rockets.jpg'/>
</body>
</html>

JQuery and frames - $(document).ready doesn't work

I have a page, with some code in js and jQuery and it works very well. But unfortunately, all my site is very very old, and uses frames. So when I loaded my page inside a frame, $(document).ready() doesn't fire up.
My frameset looks like:
<frameset rows="79,*" frameBorder="1" frameSpacing="1" bordercolor="#5996BF" noresize>
<frame name="header" src="Operations.aspx?main='Info.aspx'" marginwidth="0" marginheight="0" scrolling="no" noresize frameborder="0">
<frame name="main" src="Info.aspx" marginwidth="0" marginheight="0" scrolling="auto" noresize frameborder="0">
</frameset>
My page is loaded into the main frame. What should I do?
I have tried the method mentioned in another comment:
$("#frameName").ready(function() {
// Write you frame on load javascript code here
} );
and it did not work for me.
this did:
$("#frameName").load( function() {
//code goes here
} );
Even though the event does not fire as quickly - it waits until images and css have loaded also.
I know this is an old topic. But to help some of you who reach this page, here is my solution:
$($("#frameName")[0].contentWindow.document).ready(function() {
// Write you frame onready code here
});
I assume this is a similar problem I was having with DOMContentLoaded in an iframe.
I wrote a blog post about it.
If you want to fire the onload event for your frames, then follow these steps:
Assign an id and name to each <frame> tag. Make sure both id and name attributes value is same.
Use the following code to fire the onload event of the frame:
$("frameName").ready(function() {
// Write your frame onload code here
}
The following also worked for me:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>
$(window.parent.frames[0].document).ready(function() {
// Do stuff
});
</script>
The [0] indicates that it is the first frame in the document, [1] would be the second frame, and so on. This is particularly nice if you do not have control over the mark-up, and it is still utilizing document ready.
Have you tried to put the jQuery code inside the Info.aspx page?
Not sure what you're trying to do, but I have an even older classic asp app that operates out of frames, and I just recently added jQuery functionality and it is working great. The $(document).ready() works fine within a frame, but if you wish to reference the DOM in another frame, you'll have to use the Frame's onload event to let you know when the frame's DOM is loaded. Admittedly, I used iFrames, but the concept should be the same.
I have worked a long time with this post... here is my solution.
test.html
<!DOCTYPE HTML>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
document.write('<frameset><frame name="frame_content" id="frame_content"></frame></frameset>');
$('#frame_content').attr('src', 'test2.html');
$('#frame_content').load(function()
{
if('${"#header"}' != '') {
$("#header", frame_content.document).remove();
}
});
if($('#frame_content').complete) $('#frame_content').trigger("load");
</script>
</head>
</html>
test2.html
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="header">You will never see me, cause I have been removed!</div>
</body>
</html>
I don't know if it is the best solution, but when I remove $(document).ready() and keep its body, everything works perfectly.
No need to modify the markup. Just fix the selector. It should be:
$("frame[name='main']").ready(function(){..});
not
$("#frameName").ready(function(){..});
Note: it seems the jQuery ready event fires multiple times. Make sure that is OK with your logic.
This answer may be late, but this reply may help someone like me...
This can be done via native Javascript code -
ifrm2 = var ifrm2 = document.getElementById('frm2');
if (ifrm2.contentDocument.readyState == 'complete') {
//here goes the code after frame fully loaded
}
//id = frm2 is the id of iframe in my page
There is no reason for $(document).ready() not to be called.
Be sure your page contains an include to jquery.js. Try to do a simple test with an empty HTML page and just an alert to see if there is another problem.
If you are trying to use this inside the HTML page that contains the frame's definition, keep in mind that there is no document there, you will have to use the

Categories

Resources