Register Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools
  #1  
Old 07 May 2012, 09:55
Disco_Stu's Avatar
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
trying to get user name in widget

I'm trying to retrieve the user name - or just see if the user is signed in - in an HTML widget using javascript but it isn't working

I tried using this but it didn't work

var username =$bbuserinfo[username];


Can someone post a sample script?

Thanks
Reply With Quote
  #2  
Old 07 May 2012, 10:36
LifesGreatestGift's Avatar
LifesGreatestGift LifesGreatestGift is offline
 
Join Date: Jul 2009
cant you just use a php widget and do this

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

P.S. I doubt the code above works, its a proof of concept.

Try this if you want to stick to JS


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

not sure if single quotes are needed in JS.
__________________
If I have helped you, the least you could do is click the button to show your appreciation.
TheSecond.org - Because it is our right!

Last edited by LifesGreatestGift : 07 May 2012 at 10:49.
Reply With Quote
  #3  
Old 07 May 2012, 12:52
Disco_Stu's Avatar
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
The code doesn't work in my java function and causes the function to stop due to invalid syntax


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

I'm trying to get the user name in a Static HTML widget using java.
Reply With Quote
  #4  
Old 07 May 2012, 13:09
kh99's Avatar
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
The problem is that a static html widget is not a template, so you can't use any variables, only html. You would have to use a php direct execution type widget and set $output to your html (although getting quotes and escape characters right can be tricky). You could also make your html a template then just use a php type widget that renders your template, like:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


Then in your template you can use


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

__________________
Please don't PM me - post your questions in the appropriate forum.
Please don't PM me to ask me to read your thread.
Reply With Quote
  #5  
Old 07 May 2012, 13:13
Disco_Stu's Avatar
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
Are there any functions or API I can call from java that will return the current user name or tell me if the user is logged in? I don't want to rewrite the widget as it has a lot of java code in it.

I tried using the userid cookie but for some reason it is not always present even when I log in.
Reply With Quote
  #6  
Old 07 May 2012, 13:19
kh99's Avatar
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
Javascript runs in the browser, so it only has access to data that's included in the html document. If you look at the headinclude template there's a section that sets some javascript variables just for that purpose (so the data is available in js functions on the page). So you could edit the headinclude template and add the username there.

If you switched to a php widget with a template I don't think you'd have to rewrite the widget, you'd just copy what you have to a template and the widget code would be a few lines as posted above (if you wanted you could try it by creating a new widget so if it doesn't work out you can just delete it).
__________________
Please don't PM me - post your questions in the appropriate forum.
Please don't PM me to ask me to read your thread.
Reply With Quote
  #7  
Old 07 May 2012, 13:22
Disco_Stu's Avatar
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
I'll try the head include approach and see if that works. Do I simply assign the variable value there?

Also, any idea why I don't always see the bb_userid cookie even when logged in? At what point is it created?
Reply With Quote
  #8  
Old 07 May 2012, 13:24
kh99's Avatar
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
I don't know about that cookie. I almost suggested that in my previous post but I seemed to remember that it wasn't always set.
__________________
Please don't PM me - post your questions in the appropriate forum.
Please don't PM me to ask me to read your thread.
Reply With Quote
  #9  
Old 07 May 2012, 13:27
Disco_Stu's Avatar
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
Could you give me an example of the syntax for the header include? I set a variable to the userid but my widget doesn't seem to recognize the variable so I must be doing something wrong
Reply With Quote
  #10  
Old 07 May 2012, 13:30
kh99's Avatar
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
In the template headinclude, there's this code:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


So I was thinking of adding a line like:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


To be honest I'm not sure what special chars in a user name might cause problems with this, but in any case it seems like that should allow you to use USER_NAME in your js code.
__________________
Please don't PM me - post your questions in the appropriate forum.
Please don't PM me to ask me to read your thread.
Reply With Quote
  #11  
Old 07 May 2012, 13:33
Disco_Stu's Avatar
Disco_Stu Disco_Stu is offline
 
Join Date: Apr 2012
That one worked. It's just what I needed.

Thanks for all the help.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Jump


New To Site? Need Help?

All times are GMT. The time now is 18:39.

Layout Options | Width: Wide Color: