Jul 21
If you are using strict mode as your html document type and if you have target=_blank in your html code then wc3 gives you an error. strict mode does not accept target=_blank and they suggest to use transitional mode.
here is the javascript replacement for target=_blank. before this you have to set the rel attribute of anchor tags to “external” or anything you wish because this is used in the javascript code so see which are all the anchor tags has set the rel attribute to external .
in other words you can set the rel attribute with some value to mean that these are the tags which has to be opened in a new window.
<script type="text/javascript">
function target_blank() {
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++)
{
if ((anchor[i]href!='') && anchor[i].rel == "external")
anchor.target = "_blank";
}
}
window.onload = traget_blank;
</script>
here is a script with which you can make a single anchor tag open in new window using javascript
<a href="http://host/path.html"
onclick="window.open(this.href); return false;"
onkeypress="window.open(this.href); return false;">
Open in new window using javascript</a>
…
Jul 17
static variables in javascript
In javascript functions and objects work in the same manner. Functions are also objects.
so a function can have a member variable like in objects.
A member variable will retain its value between function calls.
So this aspect is used for having a static variable in javascript.
Actually there is no option like static variables in javascript. so here the member variable server as a static variable in javascript.
here is one simple script which uses a static variable to maintain a counter
<script>
function addmore()
{
if(!addmore.counter)addmore.counter = 0;
addmore.counter++;
alert(addmore.counter)
}
addmore();
addmore();
</script>
or here is another version
<script>
function addmore()
{
if(!this.counter)this.counter = 0;
this.counter++;
alert(this.counter)
}
addmore();
addmore();
</script>
i have used this for multiple file uploads where there will be an addmore button and a variable to maintain the count of number of uploads to display and once this.counter is greater than 10 then i alert a message that uploads cannot be more than 10.
some people had an idea that why done we use like the following instead of using it like a member function
<script>
var counter=0
function addmore()
{
counter++;
alert(counter);
}
addmore();
addmore();
</script>
my answer is not to mess up the code with too many declarations which uses the word var…
after a long time we would wonder which variable is for which function
and if the variable is with respect to the context of the process then that will a good practice of maintaing standards and more meaningful.
if you would like to suggest an alternate please comment on this because i would like to know more if there is anything beyond my scope on this.
Jun 03
Html elements are displayed cascading by default. So every element appears one after the other and it is the same with nested elements. I mean elements within another element.
I want to find the exact absolute position of an element. In other words i want to find the exact x,y of an element in the html page if you could consider the html page as a graph.
I used left and top but it will always give the x and y with respect to the parent and not to the window or the body tag which is the first parent for all visible elements. Then i read about the property offsetLeft and offsetTop but this too is with respect to the parent.
So find the parents left + its parent left + its parent till you reach the top then you will get the left of any element. The same way for finding the top that is y. here is the function which will give you the x,y/absolute position of any element.
But when i used float left property i found this function returned a different value so please check when using float left option. anyway …
function findAbsolutePosition(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
}
return [curleft,curtop];
//returns an array
}
…
and to my surprise i found the following link when i tried to find something like this to check my work.
http://www.quirksmode.org/js/findpos.html
I use this site to cross check my snippets in javascript if present. This is one useful site i have found which analyses beyond the scope.
Recent Comments