Removing a particular line from a paragraph using javascript - javascript

I pass a string from one page to another page using AJAX method.This string is available in the data base.It retrieves string along with the whole client page script of the page from where i am retrieving the data.I want the string alone from the rest of the data using javascript.The string which i retrieve from the data base alone will be keep on changing
This is how it looks:
**live its live** <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"> <head><title> Untitled Page </title>
I want to remove the whole paragraph except **live its live**.The problem is that the **live its live** text alone will be changed if it is updated in the data base
Can anybody pls help me sort out this problem....

demo
var str = '**live its live** <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> Untitled Page </title><link href="App_Themes/Modern/default.css" type="text/css" rel="stylesheet" /></head> <body> <form name="form1" method="post" action="/Web/MessageDisplay.aspx" id="form1"> <div> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTa4056JeZHQioLQvNmbYjBQvHt8A==" /> </div> <div> </div> </form> </body> </html>';
alert(str.slice(0,str.indexOf('<!DOCTYPE')))​

Your code must have a way to uniquely identify the live its live text to avoid loopholes. If you just slice the string using the content it may fail. Assuming that you used "**" as a wrapper to uniquely identify the text, an alternative to slicing the string or getting its substring is to use javascript's regular expression.
E.g.
var re = /\*\*.*\*\*/gi;
var str = '**live its live** <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"> <head><title> Untitled Page </title>;';
alert(re.exec(str)[0]);
For more information on RegExp, see https://developer.mozilla.org/en/core_javascript_1.5_guide/regular_expressions

Related

Convert jQuery element to html string

This JS code tries to modify the raw html. It does that by converting the html to jQuery element, does the modifications on the jQuery element then the part which is not working is converting back to raw html string.
Since .html() will not work with xml as indicated in the docs
How can it convert the jQuery back to raw html string? Thanks
let jQ = $($.parseHTML(raw_html));
//modify jQ to heart content
console.log(jQ.html()); //<-- undefined
The raw_html
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
//...
</html>
edit
Output of console.log($.parseHTML(raw_html));
Do
var a = $('<div>').append(raw_html);
//do modifications to variable a
$(a).html() will display the correct html
NOTE this will strip head and other tags as discussed here
heres a plnkr

Javascript regex not matching - full code included

I posted this query earlier and got the included response but I cannot seem to make this query match - I'm using Firefox. What on earth am I missing? (should be copy/pastable)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script type="text/javascript">
$('#inp1').on('keyup',function(){
$('#out1').val($(this).val().match(/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*([^\w]|_)).{8,}/));
})
</script>
Inp <input id="inp1" type="text" value="fDe^je872Fhdj"><br>
Out <input id="out1" />
</body>
</html>
match() returns Array. Use the index to grab the first and only value, which is the whole match.
$(this).val().match(/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*([^\w]|_)).{8,}/)[0]);
// index <-----------------^
And use your code inside DOM ready, since the elements are not yet present at that point.
$(document).ready(function() {
$('#inp1').on('keyup', function() {
$('#out1').val($(this).val().match(/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*([^\w]|_)).{8,}/)[0]);
})
});

html generation via javascript: avoiding htmltidy error

I have a web page that generates HTML via JavaScript.
When validating using HTML validator 0.9.5.1 in Firefox 22, I get an error: 'document type does not allow element "span" here'
I am using this JavaScript:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...<body>...
<script type='text/javascript'>
var someHtml = '<span>Hello world!</span>';
var e;
e = window.document.createElement('div');
e.innerHTML = someHtml;
window.document.body.appendChild(e);
</script>
Obviously the parser assumes that <span> is nested inside <script>
How should i rewrite the JavaScript to pass HTML validation? I would prefer a solution that does not require me to create HTML elements.
Note: The answers in Avoiding HTML-in-string / html() in a jQuery script do not help me since I know that the code works. I want to reformat to avoid validation errors.
the pre element is for preformatted text, not more HTML.
HTML Tidy's objection is that you are putting something that it believes you expect the browser to render as HTML, you need to scape the entities (replacing < and > with < and >) so that it is interpretted as text.
UPDATE IN RESPONSE TO COMMENT:
With an XHTML doctype, the document must be wellformed XML. So, you need CDATA marks inside your script tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Hello world</title>
</head>
<body>
<script type='text/javascript'>
//<![CDATA[
var someHtml = "<div>Hello world!</div>";
var e;
e = window.document.createElement('div');
e.innerHTML = someHtml;
window.document.body.appendChild(e);
//]]>
</script>
</body>
</html>
You can't put <div> inside <pre>. <pre> can contain phrasing content only, where <div> is not.
Also you should wrap your script with <![CDATA[ ... ]]> section since doctype is XHTML.

Text field without form

I have a web page and I want to do something with user input text. Normally, <input type="text" /> is used inside a <form> nesting and setting input without form attribute is not acceptable in html strict schema. I want to avoid <form>, but still want some text field to use with JavaScript.
I am seeking an example with an option for text input and alert input data on click of a button, with/without jQuery.
Using W3C's validator, this passes HTML 4.01 Strict:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
</head>
<body>
<p><input type="text" value="hi"></p>
</body>
</html>
and this passes XHTML 1.0 Strict:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
</head>
<body>
<p><input type="text" value="hi" /></p>
</body>
</html>
The problem is that <input> is an inline tag and thus needs to be inside something that accepts such tags (<body> does not).
Having a closer look at the DTD, an input element is part of the inline (lets call it) group:
<!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
<!-- %inline; covers inline or "text-level" elements -->
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
So wherever inline is allowed, so is input. That means you don't need a form.
E.g. in a div:
<!ENTITY % flow "%block; | %inline;">
(...)
<!ELEMENT DIV - - (%flow;)* -- generic language/style container -->
But not as you mentioned in a comment, in the body:
<!ELEMENT BODY O O (%block;|SCRIPT)+ +(INS|DEL) -- document body -->
(I think it goes without saying that block does not contain inline).
It's perfectly OK to have an input element without a form. Where does it say it's not allowed?

Document Type Definition in html

If i add the Js Script above This Line <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Then CSS Is not working. Is There is any way to solve this issue
<script type="text/javascript">
<?php $data3 = getmaildata(); ?>
var collection = [<?php echo $data3; ?>];
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>.:: sample ::.</title>
<link rel="stylesheet" href="css/stylesheet.css" type="text/css">
A script element can appear in the head or in the body, it can't appear before the Doctype and no element can appear outside the root element (<html>).
If the Doctype (with a couple of provisos which don't apply in this case) isn't the very first thing in a document then browsers will enter Quirks mode (and emulate bugs seen in older browsers with CSS and DOM handling).
There is no way around this (that is well supported by browsers), so just write valid code and don't try to put a script element somewhere that it isn't allowed.
<script> tags are usually placed in <head> or just before </body>, I don't know if it's related but your code is still invalid.
What happens if you put the SCRIPT element in its proper place, inside the HEAD section or in the BODY?
Also, I don't know what $data3 contains, but if it's a string and not an integer for instance, then it should be encapsulated in quotation marks.
The doctype declaration should be the very first thing in an HTML document, before the tag.
The doctype declaration is not an HTML tag; it is an instruction to the web browser about what version of the markup language the page is written in.
The doctype declaration refers to a Document Type Definition (DTD). The DTD specifies the rules for the markup language, so that the browsers can render the content correctly.
http://www.w3schools.com/tags/tag_DOCTYPE.asp

Categories

Resources