Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I need to load a advert contained in a set of script tags inside some script tags is it possible ?
When you use a <script> HTML tag what is inside is treated as a quoted (literal) string, so the inner </script> tag inside is treated as a closing tag rather than as a portion of the string. So you cannot directly use the tag inside a script section.
If you must, user2310289's approach is valid or you could also use string concatenation -
eg:
"</sc"+"ript>");
You could try some thing like
var x = '<' + '/script>'; // or
var x = '<\/script>';
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have dynamically generated HTML, for example:
const content = '<b>some <i>text</i><b>'
How can I get this content and remove last 2 letters, to get <b>some <i>te</i><b>? I'm getting this content via el.innerHTML. The main idea, I want to keep html how it is, and crop only text.
Using regex on HTML is... problematic.
An alternative method would be to create a new element, set its innerHTML to the string. Then update the text content of the i element.
const content = '<b>some <i>text</i><b>';
const span = document.createElement('span');
span.innerHTML = content;
const i = span.querySelector('i');
i.textContent = i.textContent.substring(0, i.textContent.length - 2);
console.log(span.innerHTML);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How do I encode in JS only what's inside tags but I don't know the order in which the tags will appear. I do know that I will only have em, strong, br and a tags tho. Look at the funny o and & below. Thanks.
<div>hello my <em>friend</em>, this is how you <em><strong>search</strong></em> <strong>göogle</strong> & remember, don't lose your <strong><em>googliness</em></strong>...</div>
So it should encode only the special characters inside the tags but not the tags themselves.
Desired Output:
<div>hello my <em>friend</em>, this is how you <em><strong>search</strong></em> <strong>göogle</strong> & remember, don't lose your <strong><em>googliness</em></strong>...</div>
This should help you get up & running. Simply iterate over the childNodes and modify their textContent property:
[...div.childNodes].forEach(
(el) => {
// here you can do whatever you want with your nodes
el.textContent = el.textContent.toUpperCase();
// so this is the place to implement your encoding logic
}
)
<div id="div">
hello my <em>friend</em>, this is how you <em><strong>search</strong></em> <strong>göogle</strong> & remember, don't lose your <strong><em>googliness</em></strong>...
</div>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am developing a chrome extension in which I need to clone the body tag-childrens. (a clone - so that the elements does not get updated when the DOM changes). And Re-append them to the body tag, without the script tags.
I am doing this to avoid the script tags for executing.
The problem is that I don't want script tags cloned along with the other elements in the body. I read that using regex to filter/search for stuff in the html is general not a good idea.
How can I accomplish my goals, what are my options?
A vanilla javascript approach might look like the following.
var div = document.createElement('div');
div.innerHTML = document.body.innerHTML;
div.querySelectorAll('script').forEach(function(scriptTag) {
scriptTag.parentNode.removeChild(scriptTag);
});
document.body.innerHTML = div.innerHTML;
<div>
A div
</div>
<script>
console.log('hi');
</script>
<p>
A paragraph
</p>
Using jQuery you could try something like:
var $clone = $('<div>').append($('body').html())
$clone.find('script').remove()
Then when you want to replace:
$('body').html($clone.html())
Note that all event listeners will be lost with this approach
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Just like the title says I need a way to get the HTML of a webpage with out opening it in a new tab or window. I am making a chrome extension that will take element values from one page and append them to another page.
var htmlObject = gethtml("url");
would like some thing like this
You can use jQuery to get the content of the webpage:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var htmlObject;
var yourURL = 'url.html';
$.get(yourURL, function(html) {
htmlObject = html;
});
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have the following HTML
<div id="example">
...some text...
<script type="text/javascript">
... some javascript...
</script>
</div>
How to get content of #example but also without the JavaScript tags or JavaScript code?
And to answer the general question, if you need to get the contents of a node with some of the children not included, you probably need to remove the tags you don't want in there, in your case the script tags.
If you need to keep the original intact, take a look at cloneNode (https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode) on how to get a clone of the node that you can work on without touching the original.
If you just want to grab the text that's inside the id="example", you could just use:
var getExample = document.getElementById('example').innerHTML;
or with jQuery:
var $getExample = $('#example').html();
I'm assuming you just didn't want the JavaScript inside the id="example" div, being as you tagged this JavaScript. In that case you could create a JavaScript file and add it with
<script src="javascriptFileName.js"></script>
this would be placed at the bottom of the body.
With jquery you can try this way. See here http://jsfiddle.net/AnjJa/129/
var el = $('#example').clone();
el.find('script').remove();
alert(el.html());
But why you put the JS there?