Disable right click on pdf inside embed element - javascript

I have an embed element in which I provided path to pdf file. I want to prevent it from being download.
<embed src="test.pdf" width="760" height="800" oncontextmenu="return false" />
but when I right click on that t gives me options to save and print pdf. I want to prevent these options.
I tried
<script type="text/javascript">
document.addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);
</script>
but it disables right click on entire page except for PDF.

One simple and reliable solution, that is not affected by CORS or CSP, is to cover the embed with another element. I'm using an image here because you cannot embed pdfs on stack overflow.
.embed-cover {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
/* Just for demonstration, remove this part */
opacity: 0.25;
background-color: red;
}
.wrapper {
position: relative;
overflow: hidden;
}
/* Not Important*/
img {
width: 300px
}
<h3>Normal img/embed/object element</h3>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a">
<hb/>
<h3>With cover</h3>
<div class="wrapper">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a">
<div class="embed-cover"></div>
</div>
The covering element 'catches' any click events and prevents them from reaching the underlying element (the image in this case)

You should use iframe and inject your javascript code.
To do:
Use iframe tag instead of embed tag and use myFrame as id. ex:
<iframe id="myFrame" width="760" height="800" />
In your parent document get iframe from DOM.
Call window.eval method of iframe, use
'document.addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);'
as string parameter.
Now iframe should not effected by right click.
It should look like this at the end:
var myFrame = document.getElementById('myFrame');
myFrame.window.eval('document.addEventListener("contextmenu", function (e) {e.preventDefault();}, false)');

with Wendelin's answer i was able to achieve what i wanted to achieve.
<html>
<head>
<style>
.embed-cover {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
/* Just for demonstration, remove this part */
opacity: 0.25;
}
.wrapper {
position: relative;
overflow: hidden;
}
</style>
<script type="text/javascript">
function disableContextMenu() {
window.frames["pdfframe"].contentDocument.oncontextmenu = function(){return true;};
var myFrame = document.getElementById('pdfframe');
myFrame.window.eval('document.addEventListener("contextmenu", function (e) {e.preventDefault();}, false)');
}
</script>
</head>
<body onload="disableContextMenu();" oncontextmenu="return false">
<div class="wrapper">
<embed id="pdfframe" src="<url of myfile.pdf>#toolbar=0" width="100%" height="100%" ></embed>
<div class="embed-cover"></div>
</div>
</body>

You can try to use CSS to disable clics, but it will disable the scroll bar :
iframe {
pointer-events: none;
}

Related

Get img src onclick then feed it into other function

I'm trying to get a gallery set up that, upon clicking a smaller image, it will show a hidden div with a larger size with that specific image that was clicked.
I'm wondering how you set up a Jquery where, upon clicking a div, it feeds the img src into another img tag (with a variable or otherwise).
I was playing around with something like
function getImageSrc(x) {
var x= document.getElementsByClassName("image").src,
return x;
Which I would then feed into another function, where x would be the img src from the getImageSrc function, but I just can't quite wrap my head around it. I can't seem to think of how to fire an onClick event inside the first function without throwing in an additional function inside the first one.
Any help would be great. I'll even take a whole new direction with this if this method won't work (besides plugins).
Here is the code snippet now that I have time to get to it. I'm basically trying to pass the image src into the .clicked when the image is clicked, upon which the .clicked will go from visibility: hidden to visibility: visible.
The next script that needs to run is when the .clicked div is visible and clicked, it goes back to hidden.
I'm mostly having trouble figuring out the first script.
.clicked {
visibility: hidden;
height: 100%;
width: 100%;
background: rgba(35,35,41,.9);
z-index: 100;
top:0;
}
.imgcontainer {
height: 200px;
width: 200px;
overflow: hidden;
}
<div class="clicked">
<img class="clickedimg" src="">
</div>
<div class="imgcontainer">
<img class="image" src="https://processing.org/tutorials/pixels/imgs/tint1.jpg">
</div>
Its pretty simple, Code explains itself
$(document).ready(function() {
$('.small > img').click(function() {
$('.big > img').prop('src', $(this).prop('src'));
$('.big').show();
})
});
.small {
height: 100px;
width: 100px;
}
.small >img,
.big > img {
height: 100%;
width: 100%;
}
.big {
height: 300px;
width: 300px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="small">
<img src="https://processing.org/tutorials/pixels/imgs/tint1.jpg" />
</div>
<div class="big">
<img />
</div>
You could do something like this,
function getImageSrc(x){
var x= document.getElementsByClassName("image").src;
//Call the function to append the img src to the new element
appendImageSrc(x);
}
function appendImageSrc(imageSrc){
//append the src to the new Element
document.getElementsByClassName("imageLarger").src = imageSrc;
}
Please try this code. I think this will help you.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
document.getElementById("SmallerImageURL").src = "https://upload.wikimedia.org/wikipedia/commons/1/16/HDRI_Sample_Scene_Balls_(JPEG-HDR).jpg";
});
function EnlargeImage() {
var SmallImg = getImageSrc("SmallerImageURL");
document.getElementById("EnlargedImageURL").src = SmallImg;
}
function getImageSrc(ImageClass) {
var x = $("."+ImageClass).attr("src");
return x;
}
</script>
<style>
.SmallContainer {
width: 250px;
float: left;
}
.LargeContainer {
width: 500px;
float: left;
}
.LargeContainer img,
.SmallContainer img {
float: left;
width: 100%;
}
.row {
width: 100%;
float: left;
}
</style>
</head>
<body>
<div class="SmallContainer">
<img id="SmallerImageURL" class="SmallerImageURL"/>
</div>
<div class="LargeContainer">
<img id="EnlargedImageURL" />
</div>
<div class="row">
<button onclick="EnlargeImage()">Enlarge Me</button>
</div>
</body>
</html>
I have made a small modification to your getImageSrc method. I think implementing the same in jQuery is much better.
$(document).ready(function() {
$("#open_page").click(function(){
var go_to_url = $("#redirect").find(":selected").val();
document.location.href = go_to_url;
});
});
You could do something like this

How to only load iframe block when clicking on it?

I've already tried to find out how to actually do it but the codes are always different and nothing works. I always end up ruining the link or the popup itself. So, I've got this code here:
.popup {
position:fixed;
display:none;
top:0px;
left:0px;
width:100%;
height:100%;}
#displaybox {
width:460px;
margin:50px auto;
background-color:#000000;}
.displaybox {
display:block;
position:relative;
overflow:hidden;
height:800px;
width:550px;}
.displaybox iframe {
position:absolute;}
<link><a id="object">click link</a></link>
<script>
$(function(){
$("link a").click(function(){
id = $(this).attr("id");
$(".popup:not(."+id+")").fadeOut(); $(".popup."+id).fadeIn();
});
$("a.close").click(function(){
$(".popup").fadeOut();
});
});
</script>
<div class="popup object">
<div id="displaybox"><a class="close">x</a>
<br>
<div class="displaybox"><iframe src="{theiframeblock}" height="800" frameborder="0" width="550"></iframe></div>
</div>
And I want to only load the iframe-block when I click on the "click link" link. How do I have to change the script for that? Any suggestions? :)
Update
The snippet I provided was pretty simple, so I assume when you tested it, you either missed some of the code or placed things in the wrong order, or your site is interfering somehow.
So what I did was made the primary page (index.html) with everything it needs to function on it's own. I made a second page as well (target.html) which is the test page that resides in the iframe.
Here's the DEMO
Simplified your functions by:
giving your popup an id #tgt
removed that <link> element; it's not an anchor <a> it's basically for external stylesheets
gave each anchor an empty href attribute
placed e.preventDefault() in each click function to avoid the <a> default behavior of jumping to a location.
replaced the iframe's src={..} template with the root, you can change that back, I just did that so the demo can function.
$(function() {
$("a.open").click(function(e) {
$('#tgt').fadeIn();
e.preventDefault();
});
$("a.close").click(function(e) {
$("#tgt").fadeOut();
e.preventDefault();
});
});
.popup {
position: fixed;
display: none;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
}
#displaybox {
width: 460px;
margin: 50px auto;
background-color: #000000;
}
.displaybox {
display: block;
position: relative;
overflow: hidden;
height: 800px;
width: 550px;
}
.displaybox iframe {
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
click link
<div id="tgt" class="popup object">
<div id="displaybox">X
<br>
<div class="displaybox">
<iframe src="/" height="800" frameborder="0" width="550"></iframe>
</div>
</div>
You can use:
$("#object").click(function() {
$("iframe").attr("src", "http://www.your-url.com");
});
Won't allow cross-origin requets
You can do it easily by using jQuery
Try this - https://jsfiddle.net/van06539/
HTML-
Click Link
<div id="iFrameContainer">
<iframe src="http://www.bbc.com" id="bestIframeEver" height="600" width="300" style="display:none;">
</iframe>
</div>
Javascript -
var isOpened = false;
$(document).ready(function() {
$("#openFrame").click(function() {
if (!isOpened) {
$("#bestIframeEver").fadeIn(1000);
} else {
$("#bestIframeEver").fadeOut(1000);
}
isOpened = !isOpened;
});
});
You can toggle the open / close state of the iframe

Javascript event wont fire onmouse down

I have this code for js, html and css:
window.onload = function () {
console.log("aaa");
document.getElementById("back").addEventListener("mousedown", function () {
console.log("start");
}, false);};
and
<body>
<div id="mag-glass">
<object data="worldLow.svg" type="image/svg+xml">
</object>
</div>
<div id="back">
<object data="worldLow.svg" type="image/svg+xml">
</object>
</div>
<script src="mag-glass.js"></script>
and css
#back {position: absolute;
top: 0;
left: 0;
width: 1000px;
height: 1000px;
overflow: hidden; }
#back object {
width: 2000px;
height: 2000px; }
the problem is that the mouse down event is not fired. Is it because the object is bigger than the div? I tried to make the event for other elements as well but nothing helped.
It would work i have tried this on fiddle with some in html as i didnt have mag-glass.js but your problem was with event calling so it is working.
$(document).ready(function(){
console.log("aaa");
$("#back").on("mousedown", function () {
console.log("start");
});
});
Thanks for responses, I solved it with deleting object a nd substituing it with just svg itself. This way it works

How to put img in front of svg tag

I'm using snap.svg
I have index.html
<!Doctype>
<html>
<head>
<title>MAP_TEST</title>
<meta charset="utf-8">
<script type = "text/javascript" src = "JS/jquery.js"></script>
<script type = "text/javascript" src = "JS/init.js"></script>
<script type = "text/javascript" src = "JS/snap.svg.js"></script>
</head>
<body>
<div class="comm_cont">
<div id = "svgborder">
<svg id = 'svgmain'></svg>
</div>
</div>
</body>
</html>
And init.js
$( document ).ready(function() {
var s = Snap("#svgmain");
var g = s.group();
Snap.load("SVGFILES/3k1e-test.svg",function(lf)
{
g.append(lf);
//trying to load picture... Scale button in future
$('<img />', {
src: 'PNG/plus.png',
width: '30px',
height: '30px',
id: 'buttoninrk'
}).appendTo($('.comm_cont'));
//this button must be on picture
//but in front of the picture svg element
//And i can't click the button
});
});
I played with z-indexes of #svgborder and #buttoninkr but it didn't help me.
How to put button in front of svg element?
#buttoninkr, #svgborder
{
position: absolute;
}
#svgborder
{
border:5px solid black;
z-index: 0;
margin-left:auto;
border-radius: 5px;
display: inline-block;
}
#buttoninkr
{
z-index: 1;
}
Added css code with z-indexes.
There is a reason why i'm not using svg buttons instead jquery image button.
Ok, as you can see #svgmain in front of plus.png
http://jsfiddle.net/3wcq9aad/1/
Any ideas?
Solved
#svgborders
{
position: absolute;
background-color: #535364;
border:5px solid black;
z-index: 0;
margin-left:auto;
border-radius: 5px;
display: inline-block;
}
#buttoninrk, #buttondekr, #home_btn
{
position: inherit;
top:0;
margin:10px;
z-index: 1;
}
#buttoninrk
{
right:0px;
}
#buttondekr
{
right:60px
}
EDIT: It wasn't the position of the div that made the difference, but simply adding a width and height. So the original HTML works fine as long as you add a width and height to svgborder in the CSS:
http://jsfiddle.net/3wcq9aad/4/
(Note that sometimes, the position of an element within a document can make a difference to how z-index works.)
If you put the svgborder div before the svg, then z-index will work, but you'll need to know the width and height of your SVG and set it on the svgborder div.
<body>
<div class="comm_cont">
<div id="svgborder"></div>
<svg id='svgmain'></svg>
</div>
</body>
#svgborder
{
z-index: 2;
width:330px;
height:150px;
...
}
Fiddle: http://jsfiddle.net/3wcq9aad/3/
svg does not support z-index
Use element position instead:
$('element').css('position', 'absolute');
Is there a way in jQuery to bring a div to front?

try to remove iframe id with query while click like facebook

am try to remove div when click like
my code
<div id="closebox" style="position: absolute;margin-right: 2px; opacity: 0;/* width:27px; */ /* height: 20px; */ /* overflow: hidden; */ width: 47px; height: 20px; overflow: hidden; right: 202px; bottom: 15px;">
<iframe id="closebox1" src="http://www.facebook.com/plugins/like.php?href=https://www.facebook.com/0000000&layout=button_count&show_faces=false&width=60&action=like&colorscheme=light&height=31" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:45px; margin-top:3px;padding-bottom: 0px;
padding-top: 0px; height:31px; z-index: 0; /* position: absolute; */opacity: 0;" allowtransparency="true"></iframe>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">نعم </button>
<script type="text/javascript">
$('#closebox1').click(function () {
$('#closebox1').hide();
});
</script>
i need when click into div id closebox or closebox1 remove iframe id closebox1
not working because click here it's into iframe content it's like botton not div
it's not posible with iframe like button if you use any other button it's work with your code but iframe into div it's not posible to click div because iframe take click on behalf of div.
if you click your button tag and remove iframe it's posible to remove iframe.
like this
<script type="text/javascript">
$('.btn-default').click(function () {
$('#closebox1').remove()
});
</script>
Here:
$('#closebox, #closebox1').click(function(){
$('#closebox1').hide();
});
That will hide the iframe when clicking on #closebox or #closebox1
Alternatively, do something similar but instead of hiding the iframe, change the 'src' attribute.
$("#closebox").click(function(){
$(this).children("iframe").remove();
});
Please Try This one.

Categories