1

I would like to calculate the height and width of the main content area of a blogpost server side. This is something that needs to be done for many different websites. But mostly Wordpress sites. I need the width and height off the blogpost for an algorithm as parameters.

I could ask the user for this info, but unfortunately the people who are using this website are not technical so they have no idea how to find this information (eg in a developer tool). If the numbers are incorrect the outcome of my algorithm is most likely incorrect.

First I tried if there was a way to do this with PHP, but was not able to find it. Then I thought about using Java and then use PHP to communicate with Java again. Couldn’t find anything either. Next step could be to let a user “select” a div or area and calculate it from there, but this would not be ideal, because you would need the user input every time you would like to make a calculation. One other problem with this would also be how to find the main content div / element first?

If I am able to find the main content div server side automatically I could load the blogpost in an iframe and calculate the width and height with jquery. But how would you find the main content div?

Anyone who has a way to solve this problem? I am kind of open to any solution without the user input.

1 Answer 1

2

Not going to be easy to do this server side, as you're not rendering the page.

I would suggest looking at using JavaScript to retrieve the dimensions of the Div you want, and then sending these server side from your script or from a form.

You could automate the process by triggering your JS function on page load, or trigger it using a button pressed by the user, or whatever.

For reference, you can get div height and width as follows:

var divHeight = document.getElementById('myDiv').clientHeight;
var divWidth =  document.getElementById('myDiv').clientWidth;

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your quick reply. Do you really need to render the page to get a width? How do all these javascript enabled spam bots crawl the internet? Also some of this info is already in the css or code? I am familiar with getting the width and height with javascript, but this is not what I am looking for without a way to automatically find the content div.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.