XML in HTML with FadeIn-Effects - javascript

I am currently working on a banner with different texts in different languages. The banner has to be HTML (+CSS) and JS/jQuery. I though about going with an XML for the multilingual part.
Here's my html (part of it):
<script type="text/javascript" language='javascript' src='./js/jquery-2.1.0.js'></script>
<script type="text/javascript" language="javascript" src='./js/xmltranslation.js'></script>
<script type="text/javascript" language='javascript' src='./js/jquery.lettering.js'></script>
<script type="text/javascript" language='javascript' src='./js/jquery.textillate.js'></script>
.
.
.
<h1 id="title" class="tlt" data-in-effect="fadeInLeft"></h1>
My solution with the XML file is done per jQuery:
$(function() {
var filename = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
filename = filename.split(".")[0];
var language = 'de';
$.ajax({
url: 'content.xml',
success: function(xml) {
$(xml).find(filename).each(function(){
var id = $(this).attr('id');
var text = $(this).find(language).text();
$("#" + id).html(text);
});
}
});
});
That works great so far. It displays the right phrases at the right container.
But I want to use the "fadeInUp" etc. effects, found in the jquery.textillate.js library (https://github.com/jschr/textillate).
They work great, if I have a text in the tag:
<h1 id="title" class="tlt" data-in-effect="fadeInLeft">Test</h1>
The test is fading in smoothly. But it isn't working because of the XML parsing. I think, the XML parsing is done after the page is loaded, while the Fade in effect takes place, when the page is rendered.
Has anybody a better solution? I already thought about parsing the XML with jQuery or JS and put the whole page between a -Tag and output the html parts with
document.write
but since other people in my company who only have basic html skills should work with the files aswell, I would prefer another solution.
Or does anybody know another fade in-Effect library?
Edit:
My solution (adding in the JQuery XML parsing):
$("#" + id).html(text).hide().fadeIn(1000)

You could try using this library. It's worked for me when dynamically populating pages before. you just need to make the element hidden until page load. So an easy way might be to set the elements opacity to 0 then use the setTimeout function then add the class you want to the element. an example:
setTimeout(function(){
$('foo').addClass(animate fadeIn);
},100);

Related

embed zippyshare player into a local html

I try to embed this zippyshare player
<script type="text/javascript">var zippywww="54";var zippyfile="JnIxIFUy";var zippytext="#000000";var zippyback="#e8e8e8";var zippyplay="#ff6600";var zippywidth=850;var zippyauto=false;var zippyvol=80;var zippywave = "#000000";var zippyborder = "#cccccc";</script><script type="text/javascript" src="http://api.zippyshare.com/api/embed_new.js"></script>
into a html file on my pc! When I do this and open it, it will show nothing!
Any idea to do this or why it don't work?
Thanks a lot
This is old but kind of interesting since their embed code didn't work out-of-the-box for me either.
Here's how I managed to embed the Zippyshare player into my html for anyone facing this same issue.
If you look at the embed code, there is this <script type="text/javascript" src="http://api.zippyshare.com/api/embed_new.js"></script> at the end of all the declared parameters. Copy that url http://api.zippyshare.com/api/embed_new.js into your browser and there's the iframe you are looking for... right?
This is what it looks like:
var a = navigator.userAgent||navigator.vendor||window.opera;
document.write("<iframe height=\"92\" width=\""+zippywidth+"\" frameBorder=\"0\" src=\""+window.location.protocol+"//api.zippyshare.com/api/jplayer_embed.jsp?key="+zippyfile+"&server=www"+zippywww+"&width="+zippywidth+"\"></iframe>");
Somehow, however, your webpage never receives this. So I copied that part and pasted into my html code, replacing the zippyshare script call and modifying it a bit (replacing escape characters with single quotes). So my html now looks like this:
<body>
<script type="text/javascript">
var zippywww="20";
var zippyfile="CyeL81Cn";
var zippytext="#000000";
var zippyback="#e8e8e8";
var zippyplay="#ff6600";
var zippywidth="100%";
var zippyauto=false;
var zippyvol=80;
var zippywave = "#000000";
var zippyborder = "#cccccc";
var a = navigator.userAgent||navigator.vendor||window.opera;
document.write("<iframe height='92' width='"+zippywidth+"' frameBorder='0' src='http://api.zippyshare.com/api/jplayer_embed.jsp?key="+zippyfile+"&server=www"+zippywww+"&width="+zippywidth+"' allowfullscreen></iframe>");
</script>
<!--script type="text/javascript" src="//api.zippyshare.com/api/embed_new.js"></script-->
</body>
It works, BUT I still wouldn't recommend it as it defeats the core purpose of having an api call. So this is not as much an answer as it is a very temporary fix :) while you figure it out or while Zippyshare figures out what to do with all those unused customization parameters you specify.

How to load more jquery script to DOM by jquery ajax

I am creating an application that may become very large over time. So in order to keep things simple, we have decided to keep Javascript (mostly jQuery code), CSS and html for one particular feature in one file. for example, if upload is a function, then we have all the jQuery validation and css, html for upload in one file (without head and html tags).
We have a home dashboard in which a click handler will load all the links by ajax and append to the designated DIV of class indicated by additional attribute in links called "whereTOadd". so if a link has its "WhereTOadd" attribute set to ".here" and href set to upload.php then the contents of upload.php will be added to a div of class 'here' in the same page. it is done using script given below.
But the problem i am facing is that I need to include jQuery file again in every file to get the codes working, which is a terrible thing. What can be done to avoid this?
This is html of my dashboard:
<html>
<head>
..
<script src="Path-to-jquery-min-file.php" ></script>
<script>
$( document ).ready(function(){
$(document).on("click","a",function(e){
if( $(this).attr('href'))var ajaxurl = $(this).attr('href').valueOf();
if( $(this).attr('whereTOadd'))
var target = $(this).attr('whereToadd').valueOf();
/*for ajax request */
var options= {
type: 'GET',
url: ajaxurl,
dataType: 'html',
success: function (html, textStatus){
$(target).empty().append(html);
},
error: function(xhr, textStatus){
alert('error');
}
}
$.ajax(options);
return false;
});
});
</script>
</head>
<body>
<a href="upload.php" >Upload data</a>
<div class=".here" ></div>
</body>
upload.php contains:
<script type="javascript" >
$('body').append('load successful!');
</script>
This setup will not work until I make change in upload.php as:
<script src="Path-to-jquery-min-file.php" ></script>
<script type="javascript" >
$('body').append('load successful!');
</script>
Please help in solving this because loading jQuery again and again in the same page may cause errors and conflicts. Please suggest me a better approach to what I am doing.
Thanks

add horizontal rule at the end of a and div #ID

I'm trying to add a horizontal rule as the last item in a div container. I tried
<script>
var rule = '<hr />';
$('#content-nav').append(rule);
</script>
I added this code into the header.php section of my website which is added onto multiple php files using php include. But jQuery files are also added and work fine for other jQuery plugins that have been implemented.
I'm never sure where in my html js is supposed to go. Am I putting this in the wrong place? Or is there something wrong with the code?
As long as your script comes after the jQuery script AND after the DOM is ready (or after the element is declared, but on DOM ready is preferable) your code will work:
<script src="pathToJQuery.js" />
<script>
$(function() {
$('#content-nav').append('<hr />');
});
</script>
...
<div id="content-nav">
...
</div>
NB in the above the $(function() { ... }); is shorthand for $(document).ready(function() {...});

How to include text file into javascript

Is there any way to load some text from another file into javascript, without server side code?
I was thinking to use another element to hold the text inside some comments, but I don't know how to read it's source code with javascript.
Something like:
<script src="myfile.js"></script>
<script> function readMyText() { ... }</script>
In myfile.js:
/* some text */
You can put anything you want into a script tag if you give it a "type" that's not something the browser understands as meaning "JavaScript":
<script id='Turtle' type='text/poem'>
Turtle, turtle, on the ground;
Pink and shiny - turn around.
</script>
You can get the contents via the "innerHTML" property:
var poemScript = document.getElementById('Turtle');
var poem = poemScript.innerHTML;
Here is a jsfiddle to demonstrate.
That trick is popular lately with people doing client-side page building via templates.
Without using ajax or any server code... sorry mate but you can't :(
Building on Pointy's answer, to import from local files do this:
<script src="foo.txt" id="text" type="text">
</script>
You could also use this to target an external file:
<script src="http://foo.txt"></script>

HTML/Javascript - Get image source from online file

I want to have an image in a webpage that can be changed based on the filepath stored in a file online (it doesn't matter what typer of text file - xml, .txt - whatever works best).
So I basically want to have the page retrieve the text from that file, and then use that text as the source for an image in that page.
I'm assuming this is a Javascript thing, but it doesn't matter to me, as long as it works.
Any ideas?
Thanks!!
**Edit: Forgot to mention: I'm using the code in a Google Chrome Extension, not sure that matters, as it uses regular HTML/Javascript, but it's stored on the users computer, and I want the image to be stored on my server.
**Edit2:
Just got something that seems to work very well, and I only need this in the body part of the code:
<script type="text/javascript" >
var i=0;
for (i=0;i<=FilePath.length - 1;i++)
{
document.write('<img src="' + FilePath[i] + '"/>');
}
</script>
Hope this is valid code, but it definitely seems to work here...
Its simple store the filepaths in a Javascript file , create an array in the JS file , and include all the filepaths in the array, then store the file on the webserver .
Then after that you can retrieve it using
<script src="JS_File_path_on_web_server" type="text/javascript" ></script>
After you retrieve it , you can use Javascript , I prefer jQuery , to replace the src attribute on the Image with the one from the array .
EDIT : Full version :
//Javascript web server File
var FilePath=new Array("Path1","Path2","Path3");
Create a file like this and store as many paths as you want in the array .
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">
</script>
<script src="JS_File_path_on_web_server" type="text/javascript" ></script>
<script type="text/javascript" >
$(document).ready(function()
{
$('#DisplayImage').attr('src',FilePath[0]);
});
</script>
</head>
<body>
<img src="" id="DisplayImage" />
</body>
</html>
This is a simple example , you can try learning javascript and Jquery to tweak it further .
Since my php based answer was not applicable, we can all ignore it now. :)

Categories