I cannot seem to get this script to work. Can anyone please help? The DIV's width is not defined. It just stretches across the whole page.
<html>
<head>
<style type="text/css">
#box{
height:100px;
border:3px;
border-style:solid;
border-color:#000;
}
</style>
<script>
document.getElementById('box').style.width="10px";
</script>
</head>
<body>
<div id="box"></div>
Your script is running before the <div> is rendered on the page. Try it like this:
<html>
<head>
<style type="text/css">
#box{
height:100px;
border:3px;
border-style:solid;
border-color:#000;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
document.getElementById('box').style.width="10px";
</script>
</body>
</html>
And don't forget to close your <body> and <html> tags.
To prove that it is, look at this example. I moved the script back to the <head> section and changed the width setting to run when the window is finished loading.
<html>
<head>
<style type="text/css">
#box{
height:100px;
border:3px;
border-style:solid;
border-color:#000;
}
</style>
<script type="text/javascript">
alert('test');
window.onload = function() {
document.getElementById('box').style.width="10px";
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
You'll see the 'test' alert message before the box is rendered.
The element does not exist on the page yet. JavaScript can not access/manipulate an element until it has been loaded in the DOM. You can overcome this by moving you <script> block to above the closing </body>. Or use an window.load event.
An example of the former using your code is here - http://jsfiddle.net/ycWxH/
if you will use jquery it is more easy to do that.
that is if you will only use jquery framework
here is the code
$('#box').height(10);
just a reminder, window.onload is fired when page fully loaded.
refer to http://www.javascriptkit.com/dhtmltutors/domready.shtml
<script>
function doMyStuff() = {};
if ( document.addEventListener ) {
document.addEventListener( "DOMContentLoaded", doMyStuff, false );
} else if ( document ) {
document.attachEvent("onreadystatechange",function(){
if ( document.readyState === "complete" ) {doMyStuff();}
});}
</script>
Related
I'm working on an application with modal overlays that appear within iFrames when the corresponding buttons are pressed. To close one of these modal overlays, the Cancel button is defined in the parent window this way:
Cancel
I'd like to replace this with a JavaScript function (let's call it onCancel() ) so I can reset some values if needed in addition to closing the overlay. What is the JavaScript equivalent to "#close"?
You can't close an iFrame, you either have to remove or hide it. The example below removes the iframe. If you just want to hide you can replace the last line (containing removeChild with this one frame.style.display="none"; You can then get it back by using this line frame.style.display="block";
<!DOCTYPE html>
<head>
<title>test</title>
<style type="text/css">
.top {
height: 100px;
width: 200px;
background-color: blue;
}
</style>
<script type="text/javascript">
function removeIFrame() {
var frame = document.getElementById("iframe");
frame.parentNode.removeChild(frame);
}
</script>
</head>
<body>
<div class="top" onclick="removeIFrame();"></div>
<iframe id="iframe" src="/" width="200" height="100"></iframe>
<div class="top"></div>
</body>
<!DOCTYPE html>
<head>
<title>test</title>
<style type="text/css">
.top {
height:100px;
width:200px;
background-color:green;
}
</style>
<script type="text/javascript">
function removeIFrame() {
var frame = document.getElementById("target");
frame.parentNode.removeChild(frame);
}
</script>
</head>
<body>
<div class="top" onclick="removeIFrame();"></div>
<iframe id="target" src="http://www.disney.com" width="100" height="100"></iframe>
<div class="top"></div>
</body>
The approach that works for me is to define the following JavaScript function in the parent page:
function onCancel()
{
var myIFrame = document.getElementById("myIFrame");
var myForm = myIFrame.contentDocument.myForm;
var stuffWasChanged = myIFrame.contentDocument.stuffWasChanged;
if (stuffWasChanged == "true")
myForm.action = "reset.do";
myForm.submit();
location.href = '#';
}
Note that if the stuffWasChanged flag was not set to true, then no action is defined for the form in question, so the modal overlay simply goes away without any servlet method being called.
So I've managed to link my index.html to jquery but for some reason my .slideDown code is not working. Is there something wrong with it or did I not attach jquery correctly?
Here is my html code (containing the code for .slideDown):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TEST</title>
<link rel="shortcut icon" href="../Pictures/Logo.png" />
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
<script src="jquery-2.1.1.min.js"></script>
</head>
<body>
<script>
$("#teamalpha").mouseover(
function(){
$("#teamalpha > div").slideDown(400);
});
</script>
<div id="teamalpha">
<div>
<p>AlphaGuardian</p>
<p>Owner</p>
</div>
</div>
</div>
</body>
</html>
And Here is my CSS code (stylesheet linked correctly):
#teamalpha{
font-family:Optimus;
font-size:24px;
text-align:center;
border:solid;
border-color:#000;
width:250px;
height:250px;
position:relative;
top:100px;
left:200px;
cursor:pointer;
z-index:5;
}
#teamalpha div{
position:relative;
top:-50px;
height:100px;
display:none;
width:250;
background-color:#f7931e;
z-index:6;
}
Your <script> needs to be surrounded by
// Fire on document ready event
$(document).ready(function(){
// some code here
});
OR
// Fire on window load event
$(window).load(function() {
// some code here
});
So it should look like this:
<script>
// Fire on document ready event
$(document).ready(function() {
$("#teamalpha", this).on("mouseover", function() {
$(this).children("div").slideDown(400);
});
});
</script>
OR
// Fire on document ready event
$(window).load(function() {
$("#teamalpha", this).on("mouseover", function() {
$(this).children("div").slideDown(400);
});
});
Your script was running before the page was fully loaded. Now, it'll wait till the document or the window finish loading.
I made a pen with code that worked: http://codepen.io/KK4OXJ/pen/Eagjax/
Basically, you can't fade something in that's hidden with CSS, so we need this:
$('#teamalpha div').hide();
to hide it, instead of using CSS to hide it. It needs to go right before the mousover thing, and we also need to remove the display: none; line in the CSS.
I hope this helps :)
Below code doesnt work on ipad: the page gies completely blank. Works fine on desktop browsers. If I remove the doctype, it works.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#block {
background-color:red;
width:100px; height:100px; top:300px;
position:absolute;
}
</style>
</head>
<body>
<div id="skrollr-body">
<div id="block" data-0="left:0px" data-300="left:100px">test</div>
</div>
<script src="js/skrollr.min.js"></script>
<script type="text/javascript">
skrollr.init();
</script>
</body>
</html>
Strange .. using skrollr 0.6.17. Whats wrong ?
*-pike
html, body { height:100%; }
seems to fix it. hm .. is that a bug ? am i missing a lib ?
I created a very basic page to illustrate this...
<!DOCTYPE html>
<html> <!-- manifest="cache.manifest"-->
<head>
<title>FireFox Touch TEST</title>
<style>
body {width:100%; height:100%; background-color:green;}
div.testdiv {top:0px; left:0px; width:1in; height:1in; background-color:blue;}
</style>
</head>
<body class="body">
<div id="test" class="testdiv">Touch here</div>
<script type="text/javascript">
function tStart(event)
{
alert("Touched");
}
divid = document.getElementById("test");
divid.addEventListener('touchstart', function(){tStart(event)},false);
</script>
</body>
</html>
I seem to be either doing something fundamentally wrong or there is a problem with mobile firefox 24 on android 4.2.2
Any ideas...
Try like this:
As there is no such a variable in tStart() that calls event, the browser looks if there is an event defined in the global object. In JavaScript, the global object is called window
function tStart(event)
{
alert("Touched");
}
divid = document.getElementById("test");
divid.addEventListener('touchstart', function(){tStart(window.event)},false);
Please consider this code:
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
</head>
<body>
<div id="dlgDiv" style="width:202px; height:72px; border: solid 1px grey"></div>
<iframe id="iView" style="width: 200px; height:70px; border: dotted 1px red" frameborder="0"></iframe>
<script type="text/javascript">
jQuery(document).ready(function() {
var doc = document.getElementById("iView").contentWindow.document;
doc.designMode = "On"
doc.open()
doc.write("<html><head></head><body class='some-class'>Some test text</body></html>");
doc.close();
jQuery("#iView").appendTo("#dlgDiv")
})
</script>
</body>
</html>
In IE it works fine and preserves test in the frame ("Some test text") as well as it keeps it in design mode.
In FF/Chrome/Opera it wipes out all content of the iframe - if you inspect it's DOM with FireBug you can see that iframe.body lost it's class "some-class" as well as all text and it's not in design mode.
Any ideas how to overcome this problem? The original problem is that all rich text editors fail to work in a jQuery.dialog in those browsers and I tracked the problem down to the above-mentioned fact...
It's a real show stopper for me, any help would he highly appreciated!
Thank you,
Andrey
Takes to refresh the movement (appendTo) and does not locate either the iframe:
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
</head>
<body>
<div id="dlgDiv" style="width:202px;height:72px;border:solid 1px grey"></div>
<iframe id="iView" style="width:200px;height:70px;border:dotted 1px red" frameborder="0"></iframe>
<script type="text/javascript">
jQuery(document).ready(function() {
$("#iView").appendTo("#dlgDiv");
setTimeout(function(){
var iBody = $("#dlgDiv").find('#iView').contents().find("body"); // <-----
iBody.append("<div>my bad html</div>"); // old container
iBody.empty(); // empty body in iframe
iBody.append("Some test text"); //add container
iBody.append("<div>or something right</div>"); //add container
iBody.attr("class", "some-class"); //add class to body
}, 100);
})
</script>
</body>
</html>
Edition: for it is understood