How to show div only after all the image inputs loaded? - html

I want the div "container" shows only after all image buttons in the div "inner" fully loaded. Or outer_1 and inner_1 show together after 1.jpg is loaded.
<div class="container" id="top">
<div class="outer" id="outer_1"><div class="inner" id="inner_1"><input type="image" src="1.jpg"></div></div>
<div class="outer" id="outer_2"><div class="inner" id="inner_2"><input type="image" src="2.jpg"></div></div>
<div class="outer" id="outer_3"><div class="inner" id="inner_3"><input type="image" src="3.jpg"></div></div>
</div>
I have tried the below solution I found here but couldn't help. I am totally new in programming, may I know how can I do this?
var $images = $('.inner input');
var loaded_images_count = 0;
$images.load(function(){
loaded_images_count++;
if (loaded_images_count == $images.length) {
$('.container').show();
}
});

Your code is almost correct. The issue you have is that you're using the load() method, which is used to retrieve content from the server using AJAX, not the load event, which fires on img elements when they are ready to be displayed.
To fix this use on() instead of load():
var $images = $('.inner input');
var loaded_images_count = 0;
$images.on('load', function() {
loaded_images_count++;
if (loaded_images_count == $images.length) {
$('.container').show();
}
});

Normally, loaded doesn't mean rendered.
If you develop application on framework such as Angular, It will provided rendered event for you.
In case you develop application by only pure javaScript or even with jQuery,
Use setTimeOut might be help you (just in some case).
$images.load(function(){
loaded_images_count++;
if (loaded_images_count == $images.length) {
setTimeout(function(){
$('.container').show();
}, 0);
}
});

Related

How can i change the innerHTML content when clicking on an image?

I'm quite new in coding, trying to educate myself because i'm interested. So, sorry if it's going to be a bit dumb question or not so specific or not really correct...
On my "practicing site" i'm having some navigation links, which are referring to different innerHTML contents (like different pages). I used the 'onClick' event to make them show up, for example like this:
<div class="nav" onClick="changeNavigation('a')">menu</div>
It works with texts perfectly, but my problem is that i don't know how to make the same with an image. So when i click on the image, i want to be redirected to that innerHTML page, like i did it with the text based button. I tried to do it like these two ways, but none of them worked.
<img src="picture.png" onClick="changeNavigation('a')" />
<div onClick="changeNavigation('a')"><img src="picture.png"></div>
Is it possible to make this with an image and the 'onClick' event? Or how else can i make this work?
By the way this is my script to make innerHTML show up:
<script>
function changeNavigation(id) {
document.getElementById('main').innerHTML = document.getElementById(id).innerHTML
}
</script>
I also tried to add my image an id that says 'main' like in the script this way, but with no result.
<img id="main" onClick="changeNavigation('f')" src="picture.png" />
Can you help me please? I would appreciate any answer, because i already searched about this and i didn't find anything that could've helped solve my problem and i'm really stuck right now.
(Sorry if my english isn't the best, it's not my native language.)
I have updated my answer to what you want. You need to the divs id you want to display as a parameter to the function you use for onclick. A sample is below.
var divs = ["Menu1", "Menu2", "Menu3", "Menu4"];
var visibleDivId = null;
function toggleVisibility(divId) {
if(visibleDivId === divId) {
visibleDivId = null;
} else {
visibleDivId = divId;
}
hideNonVisibleDivs();
}
function hideNonVisibleDivs() {
var i, divId, div;
for(i = 0; i < divs.length; i++) {
divId = divs[i];
div = document.getElementById(divId);
if(visibleDivId === divId) {
div.style.display = "block";
} else {
div.style.display = "none";
}
}
}
.main_div{text-align:center; background: #00C492; padding:20px; width: 400px;}
.inner_div{background: #fff; margin-top:20px; height: 100px;}
.buttons a{font-size: 16px;}
.buttons a:hover{cursor:pointer; font-size: 16px;}
img {cursor:pointer}
<div class="main_div">
<div class="buttons">
<img src="http://www.clker.com/cliparts/J/g/2/D/p/I/one-hi.png" width="50px" onclick="toggleVisibility('Menu1');"> <img src="http://www.clker.com/cliparts/E/x/J/x/m/z/blue-number-two-hi.png" width="50px" onclick="toggleVisibility('Menu2');">
<img src="http://www.clker.com/cliparts/L/H/T/b/g/N/three-md.png" width="50px" onclick="toggleVisibility('Menu3');">
<img src="http://www.clker.com/cliparts/v/G/G/A/D/s/four-md.png" width="50px" onclick="toggleVisibility('Menu4');">
</div>
<div class="inner_div">
<div id="Menu1">I'm container one</div>
<div id="Menu2" style="display: none;">I'm container two</div>
<div id="Menu3" style="display: none;">I'm container three</div>
<div id="Menu4" style="display: none;">I'm container four</div>
</div>
</div>
You can just keep all of the sections as children of #main, and selectively show them when the section button in clicked. E.g.,
HTML
<nav>
<button type="button" data-index=0>Show one</button>
<button type="button" data-index=1>Show two</button>
<button type="button" data-index=2>Show three</button>
</nav>
<main id="main">
<section>One</section>
<section class="hidden">Two</section>
<section class="hidden">Three</section>
</main>
CSS
.hidden {
display: none;
}
JavaScript
const buttons = Array.from(document.querySelectorAll('button'));
const contentBlocks = Array.from(document.querySelectorAll('section'));
function hideSections (arr) {
arr.forEach(a => {
a.classList.add('hidden');
});
}
function showSection (index, sections) {
// Just a basic check, not exhaustive by any stretch
if (index !== undefined && sections !== undefined) {
hideSections(sections);
sections[index].classList.remove('hidden');
}
}
buttons.forEach(button => {
button.addEventListener('click', () => {
const contentBlocks = Array.from(document.querySelectorAll('section'));
const index = button.getAttribute('data-index');
showSection(index, contentBlocks);
});
});
Obviously you'll have to adjust your selectors for your use case, but Here's a pen
Here's a GitHub Gist pointing to some examples I created on JSFiddle based off of your specific use case (Stack Overflow doesn't let me post links to JSFiddle directly without including code here, but it's easier to follow along/experiment entirely in JSFiddle):
https://gist.github.com/andresn/f100386f06ee28e35bd83c62d9219890
More advanced stuff:
Ideally, you'd use what's called event delegation instead of adding an onclick to every anchor (DRY = Don't Repeat Yourself is good to always keep in mind while programming and so is KISS = Keep It Simple Silly). Here is a resource explaining event delegation:
https://davidwalsh.name/event-delegate
You can even take this further by preloading all your images so they load behind the scenes when the user first loads the page:
https://perishablepress.com/3-ways-preload-images-css-javascript-ajax/

Show a loader while waiting for the iframe to load according to the link clicked, and hide the loader after iframe has loaded using AngularJS

I'm trying to show a loader while waiting for the iframe to load according to the link clicked. I use Semantic UI's loader. So far, what it can do is show the loader once the link is clicked. However, the loader will not disappear anymore, even after the iframe document has loaded.
Here is the HTML code:
<div class="drive-link">
<div ng-if = "!READY.value"> <!-- to check if the link has been clicked -->
<div class="ui active centered inline loader"></div>
</div>
<iframe name="embedded-iframe" allowfullscreen="" frameborder="0" iframe-onload></iframe>
</div>
<div class="active content">
<a target="embedded-iframe" href="{{file.link}}" ng-click="show_file_name(file)"> {{file.name}} </a>
<div ng-repeat="next_file in files | limitTo:files.length:$parent.$index">
<a target="embedded-iframe" ng-if="$index > 0 && next_file.category_id == file.category_id" href="{{next_file.link}}" ng-click="show_file_name(next_file)">{{next_file.name}}</a>
</div>
</div>
AngularJS code:
app.controller('mainController', function($scope) {
$scope.READY = {value:true};
$scope.show_file_name = function(file){
$scope.fileName = file.name;
$scope.fileDesc=file.description;
$scope.READY = {value:false};
}
});
app.directive("iframeOnload", function() {
return function(scope) {
scope.READY.value = true;
console.log(scope.READY.value)
};
});
I have a directive to iframe, called iframe-onload. I was thinking that if the anchor tag was clicked, it would somehow connected to the iframe (since clicking the anchor tag would show the embedded document anyway), and it would trigger iframe-onload, which would change READY's value. However, this was not the case, and iframe-onload would only be triggered once, when the HTML page loaded.
Note:
I'm trying to to stay away from timeouts because I don't think hard coding how long the loader will show is the best way to go.
Any help will be very much appreciated. Thank you!
Use simple approach which is that use ng show with your loader code. for example if you have loader in gif image like this
Link To be click
<img src="loader.gif" ng-show="loader"/>
and then in controller you can do like this
$scope.loader=false;
$scope.activateLoader=function(){
$scope.loader=true;
}
this is html table where my data loads.
<tr ng-if="!isDataLoading" ng-repeat="x in companies">
<td>{{x.Id}}</td>
<td>{{x.CompanyName}}</td>
<td>{{x.ContactPerson}}</td>
<td>{{x.TotalRerrals}}</td>
<td>{{TotalRegistered}}</td>
<td>{{x.TotalConverted}}</td>
<td>{{x.TotalSalesAgents}}</td>
<td class="text-green"><i class="fa fa-circle"></i> {{x.AccountStatus}}</td>
</tr>
<div ng-if="isDataLoading" style="width: 100px; margin: auto;">
<img src="assets/loader.gif" style="width: 100px; height: 100px;">
</div>
and here is my controller code
User.getCompanies().success(function(res) {
console.log('All companies', res);
if (res != null || res != "") {
$scope.customers = res;
$scope.companies = [];
for (var i = 0; i < res.length; i++) {
$scope.companies.push({
Id: res[i].Id,
AccountStatus: res[i].AccountStatus,
CompanyName: res[i].CompanyName,
ContactPerson: res[i].ContactPerson,
TotalConverted: res[i].TotalConverted,
TotalRegistered: res[i].TotalRegistered,
TotalRerrals: res[i].TotalRerrals,
TotalSalesAgents: res[i].TotalSalesAgents,
})
}
}
$scope.isDataLoading = false;
})
.error(function(err) {
console.log('error', err);
$scope.isDataLoading = false;
})
You can take this as an example. and change this code according to your need.

How can i enable a java script when a button is clicked(html)

I want to enable a java script when i press a button.
for example, the java script is -
<div id="miniclip-game-embed" data-game-name="upipe-skateboard" data-theme="1" data-width="600" data-height="457"></div>
<script src="http://www.miniclip.com/js/game-embed.js"></script>
and when i click the button U pipe,this java script should get enabled
The button and enabling code should be in html
This element write a iframe in div.
You can create a outstanding div, if you download and modify game-embed.js (iframe element) the event would fire at all times.
<div style="position: relative;">
<div style="position: absolute; z-index: 1000; width: 600px; height: 457px;" onclick="this.style.display='none'; /* put your action */"></div>
<div style="position: absolute; z-index: 900;" id="miniclip-game-embed" data-game-name="upipe-skateboard" data-theme="1" data-width="600" data-height="457"> </div>
<script src="http://www.miniclip.com/js/game-embed.js"> </script>
</div>
Try this!
Here's something you could try.
First of all you might need some kind of a reusable script injector. Below is an example that takes a URL as parameter and loads a script located at that URL.
function injectScript(src) {
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.language = 'javascript';
scriptElement.src = src;
document.getElementsByTagName('head')[0].appendChild(scriptElement);
}
Next, you probably need to enable not only the game, but the <div> container where it is going to be displayed. So here's a little helper that you can customise to your liking:
function enableTheGame(containerId, scriptSource) {
var container = document.getElementById(containerId);
if (container) {
container.className = 'enabled'; // can be anything, e.g. style.display='block'
injectScript(scriptSource); // here's our injector
}
}
Now, provided that your button has an id, let's attach to it an event listener that will enable the game. Note that it's important to wait for the page to finish loading.
window.addEventListener('load', function () {
var button = document.getElementById('SomeButton');
if (button) {
button.addEventListener('click', function () {
enableTheGame('miniclip-game-embed', 'http://www.miniclip.com/js/game-embed.js');
});
}
});
The above examples can be improved. For example, the last function could take a look at the button's data-... properties and decide which script to load and where to.

Toggle between two div couples when you click on them

I've found some Javascript code on the web for toggling between two images when clicking on them as in this example.
Now I wonder how to achieve the same result using divs with the pictures being inside the divs.
Both the small and the large image will each be the background image of a div which is inside another div that forms the border (I need to do this to be able to set the inner border radius of the image, which I can when I use an inner div and set its border radius). So I have:
<div class="bordersmallpicture"><div class="smallpicture"></div></div>
and
<div class="borderlargepicture"><div class="largepicture"></div></div>
How can I tell Javascript to toggle between those two div couples instead of images? Here is the Javascript code that I found for the images:
<script>
var imageURL = "small-picture.png";
if (document.images) {
var smallpicture = new Image();
smallpicture.src = "small-picture.png";
var largepicture = new Image();
largepicture.src = "large-picture.png";
}
function changeImage() {
if (document.images) {
if (imageURL == "large-picture.png") {imageURL = "small-picture.png";}
else {imageURL = "large-picture.png";}
document.myimage.src = imageURL;
}
}
</script>
And the HTML part:
<img src="small-picture.png" name="myimage" title="Click to resize" alt="tree">
Can anyone give me a hint how to edit this code to toggle between the div couples mentioned above? Or will a whole new code be necessary when dealing with divs?
You simply need to toggle the classes. See a running example using your images as CSS background in the classes:
<div id="border-div" class="bordersmallpicture">
<div id="image-div" class="smallpicture"></div>
</div>
The the Javascript becomes:
<script>
function changeImage() {
var currentClass = document.getElementById('border-div').className;
if(currentClass == 'borderlargepicture') {
document.getElementById('border-div').className = 'bordersmallpicture';
document.getElementById('image-div').className = 'smallpicture';
} else {
document.getElementById('border-div').className = 'borderlargepicture';
document.getElementById('image-div').className = 'largepicture';
}
}
</script>
If you expect using javascript a lot, I recommend using jQuery which would make the code easier:
<script>
function changeImage() {
$('#border-div').toggleClass('bordersmallpicture').toggleClass('borderlargepicture');
$('#image-div').toggleClass('smallpicture').toggleClass('largepicture');
}
</script>
toggleClass turns ON/OFF a class (Here is the example)

add dynamically data-role pages

I want add dynamically data-role pages in my phonegap application. I thought that I can do this with something like this but isn't working
jQuery(function()
{
var theList = jQuery('#results');
for(i=0; i<mytool_array.length; i++)
{
content = '<div data-role="page" id="page'+i+'"><div data-role="header" data-backbtn="false"></div><div data-role="content"><p>page=+'+i+'</p></div></div>';
theList.append(content);
}
})
Im my HTML:
<div id="results"></div>
As far as I can predict the problems are:
you shouldn't put pages in a div. they should be in body
your function starts at DOMready, so it is after (or partially during) jquery mobile makes its formatting
rethink your idea. putting basic html structure in body and filling them later should work better
Consider making it a list or a set of collapsibles instead of pages.
This said, your current code should look like this:
jQuery(function($)
{
var b = $('body');
for(i=0; i<mytool_array.length; i++)
{
$('<div data-role="page" id="page'+i+'"><div data-role="header" data-backbtn="false"></div><div data-role="content"><p>page=+'+i+'</p></div></div>') //newline added for readability. it shouldn't be here
.appendTo(b).page(); //newline added for readability
}
});