I have got the problem, that a window with the file content pops up though e.stopPropagation() is called. When I drag the file from the desktop in the div in my browser, my current tab is reloaded with the new content (pictures, text). As a browser I am using Chrome, but I have also tried it with Safari and Firefox!
I simply don't get it, here's my code:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>File Uploader</title>
<link href="../styles/allgemein.css" rel="stylesheet" type="text/css"/>
<link href='http://fonts.googleapis.com/css?family=Prosto+One' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<!--<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">//-->
<!--<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap-theme.min.css">//-->
<!--<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>//-->
</head>
<body style="position:relative;">
<script>
var dropZone = document.getElementById('drag_upload');
dropZone.addEventListener('drop', handleDrop, false);
</script>
<script>
$(document).ready(function () {
$("#home").button();
$("#frm_logout").button();
});
function handleDrop(e) {
e.stopPropagation(); // Stops some browsers from redirecting.
e.preventDefault();
var files = e.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
alert(files[i]);
}
}
</script>
<div style="width:100%; text-align:center; font-family: Arial; font-size:28px;"><b>Cloudservice</b></div>
<form action="../" method="post">
<input type="submit" name="logout" value="Logout" id="frm_logout" style="z-index: 1000; float:right;"/>
</form>
<input type="button" value="Home" id="home"/>
<div style="width:100%; font-size:18px; text-align:left; font-family:Arial; position:absolute; left:5px; top:100px;">
<?php echo "User: " . $user . "<br/>Maximum <br/>upload size: " . $size . " MB<br/><br/>"; ?>
<a href="fileupload">
<div
style="width: 125px; height: 40px; background-color: #003366; text-align: center; font-family: Arial; border-radius: 10px; color:#CCC;">
<div style="top: 10px; position: relative;">Fileupload</div>
</div>
</a><br/>
<div style="width: 250px; height: 435px; border: 2px solid #003366; border-radius: 15px;">
<div style="position: relative; top:200px; text-align: center;" id="drag_upload">Drop File here to upload!</div>
</div>
</div>
</body>
</html>
Deleted Answer:
You'll need to use e.preventDefault() in conjunction
with e.stopPropogation() as the default behavior is to load the file
in the current tab.
Why it's wrong is that you had preventDefault, but it's also possible the event is out of scope. e.stopPropogation() will only prevent the event from going higher up the DOM tree to parent elements. Here's some clean-up of the code that tested fine for me:
$(document).ready(function () {
$("#home").button();
$("#frm_logout").button();
});
$('body').on('dragover drop', function(e) { e.preventDefault(); });
$(document).on('draginit dragstart dragover dragend drag drop', function(e) {
e.stopPropagation();
e.preventDefault();
});
$("#drag_upload").on('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
$(this).addClass('dragHover');
}).on('dragleave dragend', function(e) {
e.stopPropagation();
e.preventDefault();
$(this).removeClass('dragHover');
}).on('drop', function(e) {
e.stopPropagation(); // Stops parent elements from receiving event.
e.preventDefault(); // Stops some browsers from redirecting.
$(this).removeClass('dragHover');
$(this).addClass('loading');
if (window.FileReader) {
if (e.originalEvent.dataTransfer.files.length<1) {
// do stuff here
}
}
else alert('Internet Explorer 9 and below are not supported for this feature.');
});
I removed </script><script> as you can just continue the previous.
I used e.preventDefault() directly on both body & document to make sure it's not going to redirect the browser, although using both e.preventDefault() and e.stopPropogation() in the event should accomplish this, I'd rather be safe than sorry.
I made the functions inline using JQuery's .on() which has proven to be the most reliable way to me of binding events.
I also added some classes that are set/unset during the events, really just copy/paste remnants from code I use, but they're useful for styling the elements when events occur. Leave them or delete them, this is more for the user than yourself.
Next, you'll want to make sure the browser is capable of window.FileReader as there is no IE support below version 10.
Last, e.dataTransfer.files does not exist. You'll need e.originalEvent.dataTransfer.files (although as you've written it I'm not sure it's that way due to the scope of the event in the different methods).
Related
In my application I have the following html with js and stylesheet:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Drag'n'Drop demo</title>
<style>
#dropArea{
min-width:50%;
min-height:800px;
background-color:#FF00BB;
float: left;
}
#imgArea {
min-width:49%;
min-height:800px;
background-color:#0F0FBB;
float: right;
}
#imgArea img {
display:block;
margin:5px;
}
</style>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script>
var onDropCallback=function(event){
event.preventDefault();
console.log("Droped")
}
var dragSrart=function(event){
// alert("DragStarted")
console.log("Drag Started");
}
var ondragoverCallback=function(event){
var elem=event.target || event.srcElement;
$(elem).css('border',"1px solid black");
}
var dragEndCallback=function(event){
event.preventDefault();
console.log("Drag End HAppened");
$("#dropArea").css('border',"none");
}
</script>
</head>
<body>
<div id="dropArea" ondragover="ondragoverCallback(event)" ondrop="onDropCallback(event)" ></div>
<div id="imgArea">
<img draggable="true" ondragstart="dragSrart(event)" ondragend="dragEndCallback(event)" src="https://kazasou.files.wordpress.com/2010/08/g288.gif">
</div>
</body>
</html>
But for some reason even though the events ondragstart and ondragend gets fired I cannot figure out why the drop event does not fire at all. I have looked on https://www.w3schools.com/HTML/html5_draganddrop.asp and on https://www.w3schools.com/HTML/tryit.asp?filename=tryhtml5_draganddrop but I cannot figure out how different is my code from the examples.
As you can see on: https://www.w3schools.com/HTML/tryit.asp?filename=tryhtml5_draganddrop you must preventDefault during on dragover event so the ondragoverCallback should have the following code:
var ondragoverCallback=function(event){
event.preventDefault();
var elem=event.target || event.srcElement;
$(elem).css('border',"1px solid black");
}
I am writing an interactive application where user performs operations using keyboard. Most of the keystrokes are captured properly and executed, but on chrome when I press (ctrl + t) a new tab opens up, instead of calling my keypress event. Where as it works perfectly on Firefox and Internet Explorer, here is the sample code
<html>
<head>
<style>
#section {
width: 80%;
height: 500px;
background: grey;
margin: auto;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#section").keydown(function(e) {
stopEvent(e);
console.log(e.key + " down");
});
function stopEvent (e) {
e.stopPropagation();
e.preventDefault();
}
});
</script>
</head>
<body>
<div id="section" tabindex="1">
<h2>Keyboard operations</h2>
</div>
</body>
</html>
See if this is of any help,
From: http://unixpapa.com/js/key.html
On keydown and keyup, the event objects also have flags that indicate
which modifier keys were being pressed when the key was typed. These
are:
event.shiftKey
event.ctrlKey
event.altKey
event.metaKey
I am new to jquery. I am trying to drag and resize a textbox using jquery. But i am facing some problems. I could drag and re-size the text box. But.. just look at the below screenshot.
To drag the text box, first i have to drag and move that "Edge" (showed in arrow mark), out of the box.
In all mean, it is not a good idea. So can somebody help me to simply drag the textbox?
below is my code
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Draggable - Constrain movement</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
#containment-wrapper { width: 300px; height:300px; border:2px solid #ccc; padding: 10px; }
#custombox { width: 160px; height: 30px; padding: 0.5em; float: left; }
</style>
<script>
$(function()
{
$("#custombox").draggable({ containment: "#containment-wrapper", scroll: false }).resizable();
});
</script>
<body>
<div id="containment-wrapper">
<div id="custombox" >
<textarea></textarea> </p>
</div>
</div>
</body>
</html>
When i tried the code in jsfiddle.net, the textbox is non-dragable. Only resize is possible. But i copied the code to notepad and run it on chrome. Then the problem in the screenshot appeared.
After drag the edge outside the textbox, the box can be dragged to anywhere in the containment.
Check this
$(function()
{
$("#custombox").draggable({ scroll: false }).resizable();
});
Fiddle
I am building a website specifically for the iPad. I am attempting to get drag & drop to work but the ondrop event seems to never fire for some reason.
Can you help me get the div to detect when I drop an element on top of it?
I have a very simple HTML page that demonstrates that the ondrop event is never fired in Safari. This fails in both desktop safari & ipad safari.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> </title>
<style type="text/css">
<!--
body {
margin: 100px;
}
.dropTarget {
width: 300px;
height: 100px;
float: left;
display: inline;
margin: 10px;
background-color: green;
-webkit-user-drop: element;
}
.drag {
width: 100px;
height: 100px;
background-color: red;
-webkit-user-drag: element; /*element*/
}
-->
</style>
<script type="text/javascript">
<!--
function onDrop(e, ele) { alert("drop"); }
-->
</script>
</head>
<body>
<div class="drag">
</div>
<br/>
<div class="dropTarget" ondrop="onDrop(e,this);">
</div>
<br/>
</body>
</html>
You need to cancel the dragover event's default action before you can use the drop event for something, otherwise the browser ignores it.
Change your dropTarget <div> from this:
<div class="dropTarget" ondrop="onDrop(e,this);">
To this:
<div class="dropTarget" ondragover="return false;" ondrop="onDrop(event,this);">
If you use inline event registration you have to use event to pass the event instead of e.
Another reason might be:
In reference with the drag-and-drop-processing-model, the drop event surprisingly does not get fired if the platform is changing the e.dataTransfer.dropEffect which (preferably should match) is not matching with e.dataTransfer.effectAllowed property of the event.
I'm trying to fire an event every time a navigation link (a.nav) is clicked. For Safari, it works as expected; however, in every other browser it ONLY fires the first time a link is clicked. For some reason, I'm not getting the desire result, is there anything I'm missing?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PatrickCason.com</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css">
<!--[if IE 7]>
<style type="text/css">
#main, #namespace {
padding-right: 115px;
}
#textarea {
padding-right: 115px;
padding-top: 15px;
}
#social {
bottom: 82px;
}
</style>
<![endif]-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
<script src="jquery.history.js" type="text/javascript"></script>
<script src="spritely.js" type="text/javascript"></script>
<script src="ajax.js" type="text/javascript"></script>
<script type="text/javascript">
$(window).load(function() {
$("#main").show("slide", { direction: "up" }, 1000);
$("#copyright").show("slide", { direction: "down" }, 500);
$("#main").effect("bounce", { times: 1, distance: 5 }, 250);
$('a.nav').live('click', function() {
//For some reason... this only works with the first time I click a nav link... herein lies the problem!
$('#spark').sprite({fps: 30, no_of_frames: 12, play_frames: 12});
});
});
</script>
</head>
<body>
<div id="floater"></div>
<div id="container">
<div id="main">
<div id="spark"></div>
<div id="namespace"><span style="font-size: 20px;">hi. i am </span><h1 style="display: inline; font-size: 36px;">Patrick Cason</h1><span style="font-size: 20px;">.</span></div>
<div id="nav">
<ul>
<li>Contact Me!</li>
<li>About</li>
<li>Links</li>
<li>Blog</li>
</ul><br>
<p><b>Phone:</b><br><span style="font-size: 24px; font-weight: bold;">615.339.4300</span></p>
<p><b>Email:</b><br>pcason#comcast.net</p>
</div>
<div id="social">
<ul>
<li><img src="images/facebook.png"></li>
<li><img src="images/twitter.png"></li>
<li><img src="images/linkedin.png"></li>
<li><img src="images/feed.png"></li>
</ul>
</div>
<div id="home"><img src="images/home.png"></div>
<div id="textarea">
<div id="content"></div>
</div>
</div>
<div id="shadow"></div>
<script>
$(window).load(function () {
$("#shadow").fadeIn(1250);
});
</script>
<div id="copyright">
Copyright © 2011 Patrick Cason. All rights reserved.
</div>
</div>
</body>
</html>
I hope I'm not being to vague... I'd really appreciate the help.
I don't see the need for using live() in your case. That is for when you have dynamically added html. Or, when you have a decent number of elements that will bind to the same event handler (rather than click() which would bind individual handlers for each link).
$('a').click(function() {
$('#spark').sprite({fps: 30, no_of_frames: 12, play_frames: 12});
});
I think the problem might be your selector. Try just selecting <a> tags
http://jsfiddle.net/EmvQk/
EDIT:
Edit your code to comment out the sprite() method and add an alert() as indicated in the comments below. I am pretty confident the problem is stemming from the sprite() call and this little test will determine if that is the case.
Your Css needs to follow the set of rules that the library requires in order to function properly. After reviewing the library example and based on your #sprite css rule ... Try making these changes:
#sprite
{
background: transparent url(images/spark.png) 0 0 no-repeat;
position: absolute;
top: 0;
left: 223px;
width: 156px;
height: 567px;
z-index: 2000;
cursor: pointer;
}
It is an issue with Spritely, as of version 0.5 they have a destroy() method, so...
$("#spark").click(function() {
var $spark = $("#spark");
$coil.sprite({
fps: 30,
no_of_frames: 12,
on_last_frame: function(obj) {
obj.spStop(); // stop the animation on the last frame
obj.spState(1);
killSprite($spark);
}
});
});
function killSprite(yourEl){
yourEl.destroy();
}
I popped the destroy method in a function that is called on the last frame of your animation so hopefully it won't cause any issues
It should be that
$('#spark').sprite({fps: 30, no_of_frames: 12, play_frames: 12});
in and of itself doesn't work multiple times. Have you tried calling that function twice and see what happens.
To test if your click handler is the culprit, add an alert to the click handler and see if the issue is your click handler or the code snipped above.
It seems to me that your problem may be with the spritely plugin for jQuery. I set up a quick test at: http://jsfiddle.net/Htm4y/, that worked for me (in Chrome). In the example, clicking the .nav links fires a jQuery toggle. I would need to see your css to figure out what is supposed to be in that div, but it must be configured correctly in order for spritely to animate it.