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 31/3/2018, 23:00

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 4/4/2018, 15:03; 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 1/4/2018, 02:47

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 1/4/2018, 13:39; 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 1/4/2018, 03:49

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 1/4/2018, 06:33

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 1/4/2018, 13:38

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 1/4/2018, 16:32

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 2/4/2018, 16:56

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 3/4/2018, 02:06

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 3/4/2018, 20:20

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 3/4/2018, 21:25

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 4/4/2018, 00:21

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 4/4/2018, 20:00

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 : 19435
Reputation : 2010
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