View Full Version : Problem with the User Login Windows
fabiano.pechibella
10-03-2012, 02:47 PM
Gurus,
Need I Help with this script. I want pick up the user login and put inside a hidden field.
I tried many, many times and the script only show me a error, where is the error in my script?
Can anyone help me?
<script language="JavaScript">
var objNet = new ActiveXObject("WScript.NetWork");
var strUserName = objNet.UserName;
var strDomain = objNet.UserDomain;
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "usuario");
input.setAttribute("value", strUserName);
document.write(strUserName);
document.getElementsByTagName("form").appendChild(input);
</script>
Tks
Fabiano Magno Pechibella
jscheuer1
10-03-2012, 03:10 PM
I don't know anything about using the modifier "WScript.NetWork" when creating an active x object or if it would then contain the values you assume it does. I do know that only IE can create an active x object, all other browsers will throw an error on that. But assuming you are using IE and that using that modifier does what you expect, when you get to this line there's a syntax error:
document.getElementsByTagName("form").appendChild(input);
It needs to have a bracketed index. If it's the first form on the page, then 0 would be the index:
document.getElementsByTagName("form")[0].appendChild(input);
And unless this code is executed after the browser parses the form, there will still be an error because it won't find the form.
The line:
document.write(strUserName);
might overwrite the page unless at that point the browser is still parsing the page. It would be better to skip it, or to use:
alert(strUserName);
There could also be other problems.
If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
fabiano.pechibella
10-04-2012, 12:11 PM
Hi John,
My idea is pick up the value of strUserName and put inside value in hidden field. When the user insert any information in form and save this form the value of strUserName go to DB.
10x
fabiano.pechibella
10-04-2012, 12:31 PM
John,
Follow the code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<!-- PEGA USUARIO LOGADO NO WINDOWS -->
<script language="JavaScript">
var objNet = new ActiveXObject("WScript.NetWork");
var strUserName = objNet.UserName;
var strDomain = objNet.UserDomain;
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "usuario");
input.setAttribute("value", strUserName);
//coloca o elemento onde vc quer
alert(strUserName);
//document.getElementsByTagName("hidden").appendChild(input);
//document.getElementsById("container").appendChild(input);
document.getElementsByTagName("form")[0].appendChild(input);
</script>
<form>
<input type="hidden" name="usuario" value="" />
</form>
jscheuer1
10-04-2012, 03:45 PM
Why would you want to append an input to the form that it already has? Rather than add an identical input with a new value, why not just change the value of the existing input?
Oh, and what I said before about this only having a chance of working in IE still goes. Other browsers will throw an error. How can you know that IE will be the only browser used?
fabiano.pechibella
10-04-2012, 04:04 PM
John,
Will I develop a intranet and the brower aproved in my job is IE. When the user access the intranet by first time i will catch the UserName login and insert in my DB. After the first access the user will win some privileges to see some areas of the intranet.
Fabiano Magno Pechibella
jscheuer1
10-04-2012, 05:46 PM
Close the browser completely. Open it and clear the cache. Hit F12 to launch developer's tools. choose the script tab. Load the page into the browser, see if anything is reported in the developer's tools. If so, copy that and post it here. If there's something you need to do with the page to cause the error, do that. If something new appears in the developer's tools, copy that and paste it here.
fabiano.pechibella
10-04-2012, 06:13 PM
John,
Follow the print the developer's tools.
Error
Detalhes dos erros da página da Web
Agente de Usuário: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Carimbo de data/hora: Thu, 4 Oct 2012 18:08:23 UTC
Mensagem: O objeto não dá suporte para a propriedade ou método
Linha: 10
Caractere: 2
Código: 0
URI: http://localhost:8500/sieb8/ActiveX.html47864787
jscheuer1
10-04-2012, 06:30 PM
What line is line 10 of sieb8/ActiveX.html?
If it's the code you posted, it's one of these:
var objNet = new ActiveXObject("WScript.NetWork");
var strUserName = objNet.UserName;
var strDomain = objNet.UserDomain;
Probably the first. So that means that type of active x cannot be used.
Otherwise it means that it can but it doesn't have UserName property.
If you click on the error in the console in developer tools, it will show you the exact line.
fabiano.pechibella
10-04-2012, 07:06 PM
John,
Follow the script from developer tool tab Script
<script language="JavaScript">
var objNet = new ActiveXObject("WScript.NetWork");
var strUserName = objNet.UserName;
var strDomain = objNet.UserDomain;
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "usuario");
input.setAttribute("value", strUserName);
document.write(strUserName);
10 - document.getElementsByTagName("form").appendChild(input);
</script>
Tab HTML
<html>
<head>
<title></title>
<script language="JavaScript">var objNet = new ActiveXObject("WScript.NetWork"); var strUserName = objNet.UserName; var strDomain = objNet.UserDomain; var input = document.createElement("input"); input.setAttribute("type", "hidden"); input.setAttribute("name", "usuario"); input.setAttribute("value", strUserName); document.write(strUserName); document.getElementsByTagName("form").appendChild(input);</script>
<body>
G0027712
Tks John for your help.
jscheuer1
10-04-2012, 07:17 PM
You need to put the index in there as I explained before:
document.getElementsByTagName("form")[0].appendChild(input);
But, as I also said, why are you appending a new input, when you could simply change the value of the existing one?
And, as I also mentioned, neither will work if the browser hasn't parsed the form yet.
I would try:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<form>
<input type="hidden" name="usuario" value="" />
</form>
<!-- PEGA USUARIO LOGADO NO WINDOWS -->
<script type="text/javascript">
var objNet = new ActiveXObject("WScript.NetWork");
var strUserName = objNet.UserName;
var strDomain = objNet.UserDomain;
//coloca o elemento onde vc quer
alert(strUserName);
document.getElementsByName('usuario')[0].value = strUserName;
</script>
fabiano.pechibella
10-05-2012, 08:11 PM
John,
Works very fine, really, really.
Thanks so much.
Hugs from Brazil :).
Fabiano Magno Pechibela
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.