Topic Owner
3 posters
Page 1 of 1
Topic Owner
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 ?
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
Re: Topic Owner
You don't need
to get the first one of the page, you could simply use something like
or
:
Here's a more extended example:
To get the topic's poster via Javascript you will need to use
to get the first page document and obtain these variables from there.
Here's how I would do it, but probably there are other ways.
|
|
|
- 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:
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- Forumember
- Posts : 144
Reputation : 111
Language : Catalan, Spanish, English
Re: Topic Owner
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
on this line
author_avatar = document.querySelector('.postprofile-avatar', d).innerHTML
I do not see any class .postprofile-avatar
Re: Topic Owner
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
The selectors change but the rest of the code is the same.
I hope now it works.
My code was meant for modernBB
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- Forumember
- Posts : 144
Reputation : 111
Language : Catalan, Spanish, English
Re: Topic Owner
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.
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
- 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
Wecoc- Forumember
- Posts : 144
Reputation : 111
Language : Catalan, Spanish, English
Re: Topic Owner
Hi Wecoc
There is no error but it shows the first post of current page as my example too, not the owner of toptic
There is no error but it shows the first post of current page as my example too, not the owner of toptic
Re: Topic Owner
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
- 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- Forumember
- Posts : 144
Reputation : 111
Language : Catalan, Spanish, English
Similar topics
» Changing Topic Owner...
» Only owner of topic can post
» can i hide the quote button from topic owner
» New owner - Does new owner inherit all old owners posts?
» So about becoming the new owner
» Only owner of topic can post
» can i hide the quote button from topic owner
» New owner - Does new owner inherit all old owners posts?
» So about becoming the new owner
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum