Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

understanding a javascript function that changes passed in <a HREF

Status
Not open for further replies.

browerv

Programmer
Jan 24, 2012
7
0
0
US
Can someone explain what's happening in this code?
Why does first alert return a blank, the second returns the path of the html file with "l" appended, the third one returns the url of the page with the parameter appended, the third returns the page url with ? appended. I'm trying to understand whats in in__obj_This.href.

Thanks.

<html>
<head>
<script type="text/javascript">
function _fnc_Build_Link(in__obj_This, selectvalue) {
var selecttype;

alert(in__obj_This.href );

in__obj_This.href = "l";
alert(in__obj_This.href );

in__obj_This.href = "?LocationFilter=" + selectvalue;
alert(in__obj_This.href);

in__obj_This.href = "?";
alert(in__obj_This.href);


}
</script>

</head>
<body>
<div >
<a onmouseover ="return _fnc_Build_Link(this,'WFD')">test_new_link</a>
</div>

</body>
</html>
 
Why does first alert return a blank,

Because your link has no href defined. So its blank.

the second returns the path of the html file with "l"
appended,

Because you are modifying the href, since its not a complete URL the browser automatically adds the server address before it. Its the equivalent of doing:
<a href="l">...</a> When you hover over it in a browser it will show the base path for the site before the L.

If there were a file named l in the server it would load that when you clicked on it.
the third one returns the url of the page with the parameter appended,

You are appending the parameter in the string. Since no page has been defined the browser uses the current one as a basis to append the parameter to.

the third returns the page url with ? appended.

That's all you are appending a ?. with no extra parameter.
I'm trying to understand whats in in__obj_This.href.

At the Start of the code in__obj_This.href is empty. Through the other modifications the browser automatically modifies the href and adds what you pass.

As you never specify a page for the href it adds the current one for all actions, unless you overwrite it with a value that is not a parameter.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you, that cleared it up for me. I understand better whats happening.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top