|
There's 2 approaches you can take
window.scrollTo(0,0);
DOM_ELEMENT.scrollIntoView(false);
Here's a little example
<html>
<body onload='createAnnoyingStuff()' onscroll='preventScroll()' style='text-align:center'/>
<script>
function createAnnoyingStuff(){
for(var i=0;i<100;i++){
document.body.appendChild(document.createTextNode('Pete and Repeat were on a boat. Pete fell out, who was left?'));
document.body.appendChild(document.createElement('br'));
}
document.body.appendChild(document.createTextNode('The End'));
}
function preventScroll(){
window.scrollTo(0,0);
}
</script>
</html>
There's no way to get to "The End" which is at the bottom of the body
Does this help at all?
|
|
|
|
|
Simeon,
Thank you for responding. I should have been more specific in my question. What I want to do is to maintain scroll position on post-back. But, strangely enough, my pages are now maintaining position on post-back. When I first had the problem several years ago, I added an AJAX update panel to solve the issue. When I recently updated one of the pages, it didn't maintain position even though it still has an update panel. Everything seems to be working fine now on all my pages, those with the update panel and those without it. So I have moved on to my next challenge. My site on GoDaddy won't run with .NET 4, even though it is compiled by VS 2010 to target .NET 4. But, this compilation still runs under 3.5.
And, I have another question for JavaScript to resolve some other issues. Maybe you will take a look at that subject, too.
Thanks for your help. I'll file your response so I can readily retrieve it.
Bobby
|
|
|
|
|
Hi,
I am trying to create a program in HTML using JavaScript. In which i want to take input from user in number and want to print the stars "*" against that number.
Kindly help and reply me with the accurate code.
Thanks
My Code is:
function PrintStars() {
var x = 0;
var y = 10;
for(x=0;x<10;x++)
{
for(y=0;y<=x;y=y+1)
{
document.getElementById("TableArea").innerHTML="<br>" + document.getElementById("etext").value;
document.write = "*";
}
}
}
;
Enter a Number:
|
|
|
|
|
<body>
Enter a Number: <input id="etext" type="text" placeholder="Enter a number here"/>
<input type="button" Value="Print" onclick="PrintStars();" />
<label id="lbl">
</body>
function PrintStars() {
var starcount = document.getElementById("etext").value;
var sx="";
for(x=0;x<starcount;x++)
{
sx +="*";
}
alert(sx);
}
|
|
|
|
|
Thank very much Sumanth. One more little thing i want to ask plz let me know how we can print stars in innerHTML instead of using alert(sx).
as i used document.write(sx).innerHTML;
but this line is replacing input box an button. why is it so?
And Please suggest me some stuff to read for understanding these kind of things as you know i am new to JS.
Again Thank you very much for helping me in this way.
|
|
|
|
|
Hi,
I suggest you to learn some basics of HTML and javascript and DOM; for starters i would say http://www.w3schools.com/ is a good place.
and regarding "document.write" your whole web page will be treated as document so you are replacing your whole html with the result with "write".instead you can add a div to your page
and in the function replace alert with
document.getElementById("result").innerHTML = sx;
But again all this is pretty basic stuff , which you can easily get on web. So its better to search on web and try yourself before posting such small questions on code project.
Happy Coding!!
|
|
|
|
|
Thank you very much for helping dear Sumanth Madireddy.
Now i will try my level best to learn basic stuff form w3school.
Thanks a lot again.
If you don't mind dear can i get your email, because the way you made me understand is quite good and easy for me.
If convenient?
|
|
|
|
|
Hi
I have a menu, and i want when i click on one of item, that item be opened and i can show the sub menu when i click on sub items, and it cloesd if i change the top menu
plz help
more thanks
|
|
|
|
|
How can we help?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I have C# code dynamically adding a textbox named txtDodic to a data table in the code behind and setting it to a Width of txtDodic.Width = (10 * 4); as so:
tc = BaseControl.CreateTableCell(tr, null, "ColDodic", null);
txtDodic = BaseControl.CreateTextBox(tc.Controls, prefix + "txtDodic", "", "ASPTextBox", "TextBoxDodic");
txtDodic.Attributes.Add("onkeypress", "ValidateDodicInput();");
txtDodic.Width = (10 * 4);
I want to change the Width of the textbox txtDodic to a width of txtDodic.Width = (10 * 16) in a JavaScript function as so:
document.getElementById('" + txtDodic.ClientID + "').Width = (10 * 15);
The problem is that when the JavaScript function is called the width does not change. Please help.
Thanks,
Steve
|
|
|
|
|
You have to use the style attr to set the width
Plus add the ID to the textbox
Now you can use javascript to program the textbox.
txtDocdic.ID = "txtDoctic"
txtDoctic.ClientIDMode = static
txtDocdic.style.add(Htmltextwriterstyle.width, (10 * 4) & "px")
|
|
|
|
|
How do I get the actual txtDocdic to set the ID, static, and style. Do I use var txtDocdic = document.getElementById('" + txtDodic.ClientID + "')? What I did was this:
Script.AppendLine(" txtDocdic.ID = 'txtDoctic'; txtDoctic.ClientIDMode = static; txtDocdic.style.add(Htmltextwriterstyle.width, (10 * 4) & 'px');");
and this did not work.
modified 27-Apr-15 10:41am.
|
|
|
|
|
there should be a property in asp.net called ClientIDMode, you set that to the value needed. Static most likely
Actually when you make a dynamic textbox, its sort of like this in c#
You have to get the server side code right before you fix your javascript.
TextBox txt_firstName = new TextBox();
txt_firstName.ID = "txt_firstName"
txt_firstName.clientIDMode = clientIDmode.static;
txt_firtName.attributes.add("onkeypress", "makeBigger(); return false;";
controls.add(txt_firstName);
Then fix your javascript.
function makeBigger(){
alert('I fired');
var txt = document.getelementbyid('txt_firstName');
txt.style['width'] = '100px';
}
Now look at your HTML source code in your browser, and double check that the ID is correct in the textbox and javascript.
|
|
|
|
|
Dear Sir,
I have a php page and with php way I get the information with below code:
$link = $_SERVER['HTTP_REFERER'];
and but now I want get this information with "document.referrer" and then send to php.
I don't know how i can send this information to php.
Best Regards
|
|
|
|
|
|
I am wrote code in jquery in onBeforeUnlaod event to show broser alert to the user. Here i have situvation to know user clicks on the "Leave the page" button or "Stay on the page" button, based on the button i need to write some code.
Please help me if any of you know about this.
Thansk for your Help.
|
|
|
|
|
Please do not post in multiple places.
Broser conformation alert[^]
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
I want to force users to enter dates in a textbox as mm/yyyy
The following works:
<html>
<head>
<title></title>
<script type="text/javascript" >
function testFormat(v) {
//var re = new RegExp("^([1-9]|1[0-2]|0[1-9])/(\d{4})$");
var re = new RegExp(document.getElementById("txtR").value);
if (v.match(re) && (v.length==7)) {alert('ok');} else { alert('oops');}
}
</script>
</head>
<body ><form runat="server" id="Form1">
<input type="text" id="txtR" value="^([1-9]|1[0-2]|0[1-9])/(\d{4})$" style="width:200px;" />
<br />
<input type="text" id="txtA" onblur="testFormat(this.value)" style="width:100px;" />
</form></body>
</html>
However:
- Why does the commented out javascript line not work? Instead I have to use this silly workaround of putting the regular expression in a text field, (well, I can make this hidden) and reference that. It's the only way I can make it work - but it's daft.
- In order to force a 2-digit month, I have added the test for v.length==7 - but it must be possible to amend the regex to check for this? Damned if I can see how...
|
|
|
|
|
Ta - but it still doesn't work. Not sure either how that particular modifier would help anyway?
|
|
|
|
|
Try this:
<script type="text/javascript">
function testFormat(v) {
var re = new RegExp(/^(1[0-2]|0[1-9])[/]{1}([2-9]\d[1-9]\d|[1-9]\d)$/);
if (v.match(re)) { alert('ok'); } else { alert('oops'); }
}
</script>
Should also take care of your "7" problem
modified 18-Apr-15 21:29pm.
|
|
|
|
|
Consider yourself raised to "Hero" status!
IDK how anyone gets their head round regular expressions. I don't pretend to be a genius, but I like to think I'm reasonably intelligent, but regex's defeat me. I'd have more chance solving a Rubik's cube blindfold.
Thank you.
|
|
|
|
|
Hi
umm.. hate to say this, but this doesn't actually work!!!
12/2015 = OK, but
12/1995 = false ??
12/2000 = false ??
any deas..?
|
|
|
|
|
for 12/1995, you see where it says: [2-9] - change it to [1-9]
It was assuming 2000+
12/2000 - just a sec... Let me check it out...
|
|
|
|
|
Ta - that bit works.. For the other - it doesn't seem to like a double-0 ...
|
|
|
|