How to capture click on iframe using javascript [duplicate] - javascript

This question already has answers here:
How to add click event to a iframe with JQuery
(13 answers)
Closed 8 years ago.
How to capture click on iframe using javascript.
I'm using this code:
document.getElementsByClassName('pin').contentWindow.document.body.onclick =
function() {
alert("iframe clicked");
}
But it is not able to capture any click of the iframe. I can only use javascript.
I'm getting this error
"SecurityError: Blocked a frame with origin"

var i = 0;
//i represents the exact node since obtaining node by class name gives array of nodes.
var iframe = document.getElementsByClassName('pin')[i].contentWindow;
or
var iframe = document.getElementById("iframe_id").contentWindow;
iframe.document.body.onmousedown =
function() {
alert("iframe clicked");
}

Related

addEventListener onerror and error event is not working [duplicate]

This question already has answers here:
Detect when an image fails to load in JavaScript
(9 answers)
Closed 5 years ago.
I have a asp.net control which is loading dyanmically with repeated controls.
I just want a alternative image if image src is not loaded or an error event is fired on an image element,but it is not working
<asp:Image ID ="test1" ImageUrl="test1.jpg" runat="server" CssClass="NotFoundMain" />
<asp:Image ID ="test2" ImageUrl="test14.jpg" runat="server" CssClass="NotFoundMain" />
$(document).ready(function () {
function callImageError(test) {
console.log(test)
alert()
test.setAttribute('src', './Image/WebSiteImage/ImageNotAvailable.jpg');
}
function gotImageElement(item) {
item.addEventListener('onerror', callImageError(item));
// item.onerror(callImageError(item))
}
var mImg = document.getElementsByClassName("NotFoundMain");
for (var i = 0; i < mImg.length; i++) {
gotImageElement(mImg[i])
}
//mImg.forEach(gotImageElement)
});
But this is not working even if image is there this event is fired and i get no image found image
Am i doing something wrong.
Please help
The event name is error, not onerror. Another issue is that you have to provide a function to be called when an event occurs instead calling it immediately.
item.addEventListener('error', function(){ callImageError(item) });

Clickable div modification...need the script to find a certain class [duplicate]

This question already has answers here:
How to select an element inside "this" in jQuery?
(2 answers)
Closed 6 years ago.
<script type="text/javascript">
jQuery(".post-entry").click(function() {
window.location = jQuery(this).find("a").attr("href");
return false;
});
</script>
Is it possible to modify this script to find a href link that has a specific class? The reason is that some of these divs have multiple links within.
Absolutely.
$('.post-entry').click(function() {
window.location = $(this).find('a.example-class').attr('href')
return false;
})
If you want a hand cursor, you'll need a bit of CSS too:
.post-entry {
cursor: pointer;
}

How do I delete an image element if it's source file doesn't exist [duplicate]

This question already has answers here:
Check if image exists on server using JavaScript?
(18 answers)
Closed 6 years ago.
I am making a site with a popup. The popup is supposed to have up to 12 images in it. I am calling my images pizza1.jpg, pizza2.jpg, pizza3.jpg, and so on.
Is there any way with pure JavaScript to make the images show up only if there is actually a file with the name I have told it to look for?
This Question is similar, but all the answers are complicated, and only moderately related.
You can use onError event for remove object from your popup:
<img src="src" onError="removeElement(this);" />
Check if it will work:
function removeElement(element) {
element.remove();
}
You should handle the onerror event of the img element.
You can create an Image instance and use its onload event to see if it has loaded. If so just append the image to whatever element.
Demo
var imgurls = [
"https://placekitten.com/g/64/64",
"https://placekitten.com/g/32/32",
"https://placekitten.com/g/none/200",
"https://placekitten.com/g/100/100",
"https://placekitten.com/g/24/24"
];
imgurls.forEach(function(url){
let img = new Image();
img.onload = onImageLoad;
img.src = url;
});
function onImageLoad(){
document.body.appendChild(this);
}
<div id="container">
</div>

trying to target a button created by JQuery [duplicate]

This question already has answers here:
Direct vs. Delegated - jQuery .on()
(6 answers)
Closed 7 years ago.
I'm trying to target a button I've created by pressing another button. My code technically works, but due to the size of what I'm doing, I'm trying to put the function of the second button press in another js file. That where I'm running into problems. My code is below.
$("#webCoding").on("click", function() {
if ( $( ".webCodingID" ).length ) {
return false;
}
picture.attr("src", "img/webCoding.jpeg");
$(".target").empty();
$(".jumbotron").hide();
var buttonGroup = $('<div role="group" aria-label="...">').addClass('btn-group-vertical center-block')
var buttonPhilosophy =$("<button type='button'>").addClass("btn btn-default btn-lg").append("Design Philosophy")
var buttonStack =$("<button type='button'>").addClass("btn btn-default btn-lg").append("Visit My Stack Overflow Account")
var buttonGithub =$("<button type='button' id='git'>").addClass("btn btn-default btn-lg").append("Look at what I've been doing on Github")
var webCodingID =$("<div>").addClass("trainingID")
$(buttonGroup).append(buttonPhilosophy).append(buttonGithub).append(buttonStack);
$('.target').append(buttonGroup).append(webCodingID);
// code for pressing created button is below.
$("#git").on("click", function() {
prompt("herro");
});
});
but I put this in another file (and get rid of the same code in the original js file), and nothing happens.
$(document).ready(function(){
$("#git").on("click", function(){
prompt("jungle boogie");
});
I can't figure out why. The js file is linked to my main page, I just can't get it to recognize buttons created by JS in another file.
You should bind the on to the body like so:
$("body").on("click", "#git", function(){
That should solve your problem I believe :)

How to call ajax while scrolling inner scroll of div [duplicate]

This question already has answers here:
jQuery : how to determine that a div is scrolled down
(7 answers)
Closed 9 years ago.
I want to call ajax function while scrolling inner scroll and append the results at the bottom of the existing results.
For refrence I've attached an image here.
How can I achive this?
You can use something like this without jQuery also
window.onload = function(){
if(window.addEventListener){
document.addEventListener('DOMMouseScroll', moveObject, false);
}
var myDiv = document.getElementById("myDiv");
myDiv.onmousewheel = ajaxFunction();
}

Categories