FF View Source | Script highlighted red - javascript

FF14 is highlighting my script tag red on a view->source
out of all my tags, just this on here:
<script type='text/javascript'>
Is there something it doesn't like ?
Thanks.

Try placing your script within the HTML tag. Perhaps it doesn't like that it is out there on its own.

SGML/HTML permit only one root element. In HTML, that should be html. You, however, have put the script element after the html element is closed.
It's not valid for an element to thus appear outside the root element. A script tag should occur inside the head or the body. (It shouldn't stop it from working, because browsers are very lenient, but it's not valid.)

Related

When I add script code in wordpress' text editor, nothing shows up

For example this code:
https://www.w3schools.com/code/tryit.asp?filename=GN64U1L3MB4D
It's a working code as you can see, but when I add it in the "text" page NOT the "visual" tab in wordpress's post's editor, nothing shows up in the preview.
Should I add something or what?
I suppose you are talking about the "classic", old editor in Wordpress, not the block editor which it has since version 5?
If you only add a script in there, i.e. starting with an opening <script> tag and ending with a closing </script> tag, you won't see anything. You need some HTML code into which the script can put its resulting content, like the first line in the example you linked to. Still, also in this case I would rather use a div tag instead of that <t> tag which is used in that example. So that HTML line (outside the script tag) would be:
<div id="time"></div>
ADDITION:
The problem is that the WP editor (also the "text view" editor) automatically adds <p> tags around each line of code that doesn't start with any other HTML tag (like <div>).
So each line of your script code is be wrapped in <p>...</p> tags and therefore doesn't work as supposed anymore, since as a script that doesn't make sense anymore. (You can see that in the source code when you inspect the page).
To avoid that, write the whole script into one line, like this:
<div id="time"></div>
<script>var theDate = new Date(Date.parse('01/17/2021 20:00 UTC'));document.getElementById("time").innerHTML = theDate.toLocaleTimeString();</script>
Now it works (I tried it).
An alternative method is to install the "TinyMCE advanced" plugin which has as an option to not insert <p> tags automatically.

How to prevent Showdown from removing script tags?

I was testing out something and tried to put this into Showdown:
<script>alert("hacked!");</script>
Of course it didn't alert anything (Showdown is made to protect against those sorts of things), but the <script> tag gets removed completely. I am using this for a user description, so the script tag (and it's contents) should be visible, just not executed.
I was thinking that most likely I would need to change some built in Showdown code but couldn't find any place in it's code that I should change to only show the script tags but not execute them.
Does anyone know any existing options or some changes to the source code to show this?
I found this place in the code and it is responsible for hashing HTML tags such as: <script> or </script>.
https://github.com/showdownjs/showdown/blob/a9f38b6f057284460d6447371f3dc5dea999c0a6/src/subParsers/makehtml/hashHTMLBlocks.js
I think you should only delete lines 329 in the file
https://github.com/showdownjs/showdown/blob/a9f38b6f057284460d6447371f3dc5dea999c0a6/src/converter.js
And everything should work.
I found the answer: I just needed to replace the start and end of the <script> tags with something visible, like <script and </script>
This is the code I used:
myshowdownhtml.split("<script").join("<script").split("<"+"/script>").join("</script>");

Inline Javascript is breaking my HTML/CSS

This is my first attempt to use jquery, javascript and HTML/CSS in one site. I have a working inline script tag that utilizes jquery and javascript. Unfortunately I can't seem to create ANY structure on the page (an h1 title, an aside, moving the rendered javascript anywhere...) while this script is in my index.html. I've done lots of research but there is some fundamental I'm missing here as I can have only one or the other. Can anyone point me in the right direction? Here's a curtailed section:
<body>
<script>
$(document).ready(function(){
var $body = $('body');
$body.html('');
//code code code
}
});
</script>
I've tried putting my html within the body and outside of the script, in the script but outside of the jquery function, and inside the jquery function. How could I get a simple "Hello World" up on this page?
What's happening is that once the page is finished loading, or ready, you are setting the HTML to nothing via $body.html('').
You could try changing it to $body.append('This was generated by jQuery!') and you'll see your hardcoded HTML in addition to some text.
Your Script is a Child of the Body, which inner html you are clearing by asigning an empty String - deleting your inline Script in the process. Have you tried moving your Script to the Head section?

Script tags in TinyMCE fields are not saving correctly

I am running tinyMCE on my site and I'm trying to insert script tags. I am trying to insert my script tags into the body of the source of the tinyMCE instance.
However, when I do I get problems. When I submit the form including the tinyMCE element It strips the tags.
I have tried the following lines below with still no success.
allow_script_urls: true
valid_elements : '*[*]'
extended_valid_elements: 'script[language|type]'
One thing I have noticed though that if I have to post the script code to the section of the source code of the tinymce instance and it works. However, I need to be able to add it to the <body> of the tinymce instance so I can see previews etc of it.
Below is the script tag I'm trying to upload.
<script type="text/javascript" src="http://resources.32red.com/ad.aspx?pid=237638&bid=3344"></script>
Can anyone shed any light on this?
Thanks
Try the following:
extended_valid_elements: 'script[language|type|src]'
I see that you've already tried updating the extended_valid_elements property using
'script[language|type]' but you forgot to allow the src attribute for the script tags
so right now the src attribute is being stripped out.
There seems to be a bug where if a script tag is wrapped in a div TinyMce dumps the script.
(even if you have extended_valid_elements correctly)
TinyMCE can be very annoying to use
What I did to solve the problem was putting the script block inside a tag <code> tag.
<code>
<script>alert("abc");</script>
</code>
I ended up with this method
hiding from tinymce all inside script tag
protect: [/<script>[\s\S]*?<\/script>/g]
check https://regex101.com/r/cH35d9/1 for regexp:
\s - all space
\S - all not space
*? - repeat to first of </script>
and this is muililine because group of []* ignoring newline borders
Supports tinymce5, tinymce6 - https://www.tiny.cloud/docs/tinymce/6/content-filtering/#protect

Is it bad to have script tag inside div?

What is bad about having a script tag inside div inside body?
I'm dynamically updating a div to reload a javascript code inside a div. Are there any issues to worry about ?
Edit
As #Bergi insisted on seeing the code. Here it is(see below). This div (along with other div(s) containing presentation HTML elements) are updated via AJAX. This script inside div contains maps to do processing of newly loaded HTML elements on page with raw data.
<div>
<script type="text/javascript">
var namesMap = <dynamic string from server here>;
var addressesMap = <dynamic string from server here>;
</script>
</div>
It is perfectly ok to place the <script> tag anywhere in the body of the document.
From here,
The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document.
However, whenever a <script> tag occurs, it pauses the parsing of the code till the script gets loaded, and executed.
You can add <script></script> inside a DIV tag. Just check on w3c, it is valid HTML.
There is not much bad about it. Most widgets work this way. It is still valid HTML.
If you want to embed an AdSense unit in your page, you will need to do it. The same with Amazon widgets. That means majority of websites have a script tag inside div.
There are pros and cons for putting scripts inside html. The good thing is that a small script can be placed close to where it is used so you can more easily understand what the page is doing.
If nobody else but that one location needs that script then it is fine to put it there, I think.
Bad thing is that when you divide parts of your program into multiple locations it becomes more difficult to see and manage how such parts interact and interfere with each other. Whereas if you keep your html and javascript in separate files it becomes easier to understand each independently, and then finally focus on how they interact with each other. What are the "interfaces" between them.
If JavaScript is interspersed into the html then you can not organize your script-code separately from organizing the HTML.
ONE MORE THING to be aware of: If you have a DIV you may think that you can manipulate its content by re-assigning its innerHTML. And that works, except, you can not inject a script into the DIV that way. SEE:
Can scripts be inserted with innerHTML?
So one bad thing about having a script inside a DIV is that you can not replace such a script by re-assigning its innerHTML.
By SCRIPT inside <DIV> still working.
But some annoy with your layout - shacking when scroll.
Best solution: put script inside <body> or <head> :D
It was always a good practice to try to put your <script></script> tags in <head></head>. However, lately arguments appearead whether putting a tags at the end of <body></body> tags, just before made a page more faster.
I would recommend to put your <script></script> in <head></head> section of your HTML document, since it is more preffered. Additionally, putting a <script></script> inside a DIV is not a good practice.
You can post your example for a better answer abour organizing the structure of your document.
To sum up, there is no problem in what you are doing. But a more organized way is what I suggest.

Categories

Resources