The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Topic Owner

3 posters

Go down

Solved Topic Owner

Post by TamDonCo March 31st 2018, 11:00 pm

Hi All

I would like to find the topic owner (who creates topic or thread), my code finds the first one in the page so it only works on page 1 and of course it does not work on page 2 ... Do you have any idea ?


Code:
  $('.postprofile dt').each(function(){
 
  var tOwner = $('.postprofile dt:eq(0)').text();
  console.log(tOwner);
}


Last edited by TamDonCo on April 4th 2018, 3:03 pm; edited 1 time in total
TamDonCo
TamDonCo
Forumember

Posts : 427
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc April 1st 2018, 2:47 am

You don't need
Code:
each
to get the first one of the page, you could simply use something like
Code:
$(selector)[0]
or
Code:
document.querySelector(selector)
:

Code:
var owner = $('.postprofile dt')[0];
var owner = document.querySelector('.postprofile dt');

Here's a more extended example:

Code:
var author_avatar = document.querySelector('.postprofile-avatar').innerHTML,
    author_link = document.querySelector('.postprofile-name').innerHTML,
    author_name = document.querySelector('.postprofile-name').innerText;

To get the topic's poster via Javascript you will need to use
Code:
$.get
to get the first page document and obtain these variables from there.

Code:
var href = window.location.pathname.replace(/p(\d*)?-/, '-');
var author_avatar, author_link, author_name;
$.get(href, function(d){
  author_avatar = document.querySelector('.postprofile-avatar', d).innerHTML;
  author_link = document.querySelector('.postprofile-name', d).innerHTML;
  author_name = document.querySelector('.postprofile-name', d).innerText;
}).done(function(d) {
  // Here do whatever you want with author_avatar, author_link, author_name
  console.log(author_avatar);
  console.log(author_link);
  console.log(author_name);
});

Here's how I would do it, but probably there are other ways.


Last edited by Wecoc on April 1st 2018, 1:39 pm; edited 1 time in total
Wecoc
Wecoc
Forumember

Male Posts : 144
Reputation : 111
Language : Catalan, Spanish, English

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo April 1st 2018, 3:49 am

Thanks Wecoc

I will try later and let you know
TamDonCo
TamDonCo
Forumember

Posts : 427
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo April 1st 2018, 6:33 am

Uncaught TypeError: Cannot read property 'innerHTML' of null

on this line

author_avatar = document.querySelector('.postprofile-avatar', d).innerHTML


I do not see any class .postprofile-avatar
TamDonCo
TamDonCo
Forumember

Posts : 427
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc April 1st 2018, 1:38 pm

Sorry, I thought it was the same in phpBB3 but now I see the profile parts don't have a specific class there.
My code was meant for modernBB Confused

The selectors change but the rest of the code is the same.

Code:
author_avatar = document.querySelector('.postprofile dt', d).firstChild.innerHTML;
author_link = document.querySelector('.postprofile dt', d).lastChild.innerHTML;
author_name = document.querySelector('.postprofile dt', d).lastChild.innerText;

I hope now it works.
Wecoc
Wecoc
Forumember

Male Posts : 144
Reputation : 111
Language : Catalan, Spanish, English

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo April 1st 2018, 4:32 pm

Still the same error
TamDonCo
TamDonCo
Forumember

Posts : 427
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc April 2nd 2018, 4:56 pm

I tried it in 2 different phpBB3 forums and it worked well in both, the only issue I found is you have to be logged in to get the user's link, so now I've put an escaper when that occurs. This should work, the only reason this may not be working would be if you have a very edited viewtopic_body.

Code:
var topic_location = /\/t(\d*)/.test(window.location.pathname);
// Return if you are not viewing a topic
if (!topic_location) return false;
var href = window.location.pathname.replace(/p(\d*)?-/, '-');
var author_avatar, author_link, author_name;
$.get(href, function(d){
  author_avatar = document.querySelector('.postprofile dt', d).firstChild.innerHTML;
  author_link = document.querySelector('.postprofile dt', d).lastChild.innerHTML;
  author_name = document.querySelector('.postprofile dt', d).lastChild.innerText;
}).done(function(d) {
  // Return if avatar or link are not obtained (user is a guest)
  if (!author_avatar || !author_link) return false;
  // Here do whatever you want with author_avatar, author_link, author_name
  console.log(author_avatar);
  console.log(author_link);
  console.log(author_name);
});

Also, I don't know where are you inserting this and for what reason exactly, but it must be inside a function.
If you don't put it inside a function you will get this error: Uncaught SyntaxError: Illegal return statement

That's all I can say and do about this, if you still have troubles you will have to give more details on the complete code and what are you trying to accomplish.
Greetings Wink
Wecoc
Wecoc
Forumember

Male Posts : 144
Reputation : 111
Language : Catalan, Spanish, English

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo April 3rd 2018, 2:06 am

Thanks Wecoc

I will try later, I am busy now
TamDonCo
TamDonCo
Forumember

Posts : 427
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo April 3rd 2018, 8:20 pm

Hi Wecoc

There is no error but it shows the first post of current page as my example too, not the owner of toptic
TamDonCo
TamDonCo
Forumember

Posts : 427
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Wecoc April 3rd 2018, 9:25 pm

There is no error but it shows the first post of current page as my example too, not the owner of toptic

Luckly now I know why Laughing

Code:
var topic_location = /\/t(\d*)/.test(window.location.pathname);
// Return if you are not viewing a topic
if (!topic_location) return false;
var href = window.location.pathname.replace(/p(\d*)?-/, '-');
var author_avatar, author_link, author_name, postprofile;
$.get(href, function(d){
  postprofile = $('.postprofile dt', d)[0];
  author_avatar = postprofile.firstChild.innerHTML;
  author_link = postprofile.lastChild.innerHTML;
  author_name = postprofile.lastChild.innerText;
}).done(function() {
  // Return if avatar or link are not obtained (user is a guest)
  if (!author_avatar || !author_link) return false;
  // Here do whatever you want with author_avatar, author_link, author_name
  console.log(author_avatar);
  console.log(author_link);
  console.log(author_name);
});

The trouble was with the error, now you don't have that error this is plain sailing.
Wecoc
Wecoc
Forumember

Male Posts : 144
Reputation : 111
Language : Catalan, Spanish, English

Back to top Go down

Solved Re: Topic Owner

Post by TamDonCo April 4th 2018, 12:21 am

Thank You very much Wecoc


It works now .. héhé
TamDonCo
TamDonCo
Forumember

Posts : 427
Reputation : 2
Language : English

http://nhomcho.forummotion.com/

Back to top Go down

Solved Re: Topic Owner

Post by Ape April 4th 2018, 8:00 pm

Problem solved & topic archived.
Please read our forum rules: ESF General Rules


Topic Owner Left1212Topic Owner Center11Topic Owner Right112
Topic Owner Ape_b110
Topic Owner Ape1010
Ape
Ape
Administrator
Administrator

Male Posts : 19109
Reputation : 1991
Language : fluent in dork / mumbojumbo & English haha

http://chatworld.forumotion.co.uk/

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum