1

So I have been wondering how can I manipulate DOM in languages other than Javascript and PHP. I can always use Javascript and PHP in one HTML file with other HTML and CSS code.

Similarly, in Javascript there are multiple methods such as document.getElemenyById() etc. In PHP, basically it is a hypertext pre-processor and thus it automatically renders HTML through the server which again can be used for DOM Manipulation.

What about other languages? Can we use them to build the web? Or specifically, do things like HTML and CSS Manipulation. Let's say for example, Python or C#. How can I use them like JS and PHP?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Parker Queen
  • 619
  • 2
  • 12
  • 27
  • http://stackoverflow.com/questions/2782097/python-is-there-a-built-in-package-to-parse-html-into-dom – Ahmed Ginani May 01 '17 at 09:56
  • I think at the end all turns into javascript. They transpile. – TuralAsgar May 01 '17 at 10:03
  • PHP is server side scripting language. Similarly, in place of PHP one can use Python, Java or any other server side scripting language also to do the same thing. But, DOM is part of client side thing & at client side except for Javascript no other kind of scripting works. So, except javascript nothing can help you to make anty kind of changes on DOM. – Suresh May 01 '17 at 10:03
  • @TuralƏsgərov That is not the question here. I am asking how can I use, lets say Python, to manipulate the HTML. Like..what is the alternative of 'document.getElementById() in Python? – Parker Queen May 01 '17 at 10:05

2 Answers2

3

There are two stages of "working" with DOM. The first stage is the server render. When PHP engine receives template (mix of HTML, PHP) plus data and outputs valid HTML to a browser. In this stage, you can build HTML that will be parsed by a browser to DOM tree. You don't work with DOM, but rather with a plain text of your template.

The second stage is the Javascript evaluation. A browser parses and runs JS, and this JS has access to DOM tree.

The first stage can be done in any language that runs on a server. You can use c# templates, python templates, go templates, even javascript. All template engines just proceed template (text) and data and give text output which can be valid HTML.

The second stage works only with JS because browsers work with JS only. There were some attempts to bring other languages to a frontend (like Dart), and all failed. But you have some options. There are many compilers for existing languages that can target JS. For example, clojruescript for clojure, bucklescript for ocaml, gopherjs for go and others. Also, there are bunch of languages which created for this purpose (compile to JS): purescript, elm, typescript, etc. Modern browsers can use webassembly binary packages. It differs from JS, but also don't have access to DOM.

Dmitry Manannikov
  • 1,124
  • 1
  • 10
  • 14
  • Thanks buddy. Now I understand why JS is Client-Side. Because, the browsers have it by default. And if Python/C# etc had been implemented in browsers, we could do things with them as well the same way we do with JS. One last thing, why cant we use Python in the same file along with HTML the same way we do with PHP? – Parker Queen May 01 '17 at 10:31
  • In fact for OCaml i would have cite [ocsigen](https://ocsigen.org/) which allow you to work on both client and server side in a unified way. – ghilesZ May 01 '17 at 10:48
  • @HenrySpike it because of an architecture of language. I'm not familiar with Python ecosystem, but in Nodejs some template engines allow using js code inside. Maybe there is something in Python. But I should notice: this is a bad practice. Check the MVC pattern. It helps you to avoid common pitfalls with PHP. – Dmitry Manannikov May 01 '17 at 11:51
1

The real question you have raised here is:

Whether we can use languages like C# or python for DOM manipulation like JS?

In simple words, the answer is no. But if you are using any kind of parser, you will get a feeling that you are doing something in python like HTML but again on browser it will be HTML only. Rather I will say... there is very big difference between mark up language (front end language) and server side language.

HTML is used as a markup to show some content on web page. You can use some templating though but that will again finally be compiled into HTML only. Now comes the role of JS, whatever elements have been created on web page, Javascript has power to perform operation/manipulation on them. This is called DOM manipulation which is only possible through some front end language. And currently we have only Javascript as per my knowledge (Please somebody correct me if I am wrong)

Now comes the role of C#, python or PHP. These languages are for back end jobs. Anything you are sending from front end, will be processed at backend and these languages are used for those purpose so, DOM manipulation thru C# is a big NO. You need javascript (jQuery or other library) for that.

Thanks, Hope it helps.

Nitesh
  • 1,490
  • 1
  • 12
  • 20
  • So, the way to do it would send the data from 'Python' etc to JS and then JS can manipulate DOM? Moreover, Why does the PHP work with the HTML but other langs dont? Furthermore, who decided that JS would be client-side. I mean its afterall a programming language just like all others.? – Parker Queen May 01 '17 at 10:08