Not Working .innerHTML - javascript

I am a seller on eBay and I want to use JavaScript on eBay. I have successfully used document.write to put the source file of JavaScript. The problem is it not working
This is my JavaScript:
window.onload = function trackt(){
if (typeof this.href === "undefined") {var str = document.location.toString();}
else {str = this.href.toString();}
var pos = str.indexOf("itm/");
var newStr = str.substring(pos+4);
var pos1 = newStr.indexOf("-/");
var newStr1 = newStr.substring(0,pos1);
var newStr2 = newStr1.replace(/\-/g,' ')
document.getElementById("cssmenu1").innerHTML = newStr1;
}
This my css:
#cssmenu_box{}#cssmenu1{float:left;right:-2%;padding:9;width:940px;height:31px;font-size:25px;background:transparent url('http://www.endition.net/ebay/templates/001/images/menu_bg.jpg') repeat-x top left;font-family:'Trebuchet MS',Helvetica,Arial,Verdana,sans-serif;color:#FFFFFF;border: 0.5px solid;border-radius:10px;}
This is my html code:
<div id="cssmenu_right"></div><div id="cssmenu1">Men Leather Jacket</div>
.innerHTML is showing error when I run it using console. I have to use document.write() function.

window.onload = function trackt(){
if (typeof this.href === "undefined") {var str = document.location.toString();}
else {str = this.href.toString();}
var pos = str.indexOf("itm/");
var newStr = str.substring(pos+4);
var pos1 = newStr.indexOf("-/");
var newStr1 = newStr.substring(0,pos1);
var newStr2 = newStr1.replace(/\-/g,' ')
document.getElementById("cssmenu1").innerHTML = newStr1;
}
//instead of innerHTML use text(newStr1);

Related

How to get input text field values in print preview?

Here is a sample code what I am trying to fix. In print screen I am not able to see the text which is entered in input text field. Any solution will be appreciated.
var divToPrint=document.getElementById('div1Id');
if (divToPrint != null && divToPrint != "") {
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
newWin.close();
}
use jquery.print.js library and call this function in your custom function $.print("#id");
Textbox and Checkbox controls you have to get value instead of InnerHtml
var divName = document.getElementById('div1Id')
var originalContents = document.body.innerHTML;
var inpText = document.getElementsByTagName("input")[0].value;
var printContents = document.getElementById(divName).innerHTML;
printContents += inpText;
newWin.document.open();
newWin.document.write('<html><body
onload="window.print()">'+printContents+'</body></html>');
newWin.document.close();
You have this script
var divToPrint=document.getElementById('div1Id');
if (divToPrint != null && divToPrint != "") {
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
newWin.close();
}
where you copy the inner html, however, the value is not there. Let's make sure the values are correct, like this:
var divToPrint=document.getElementById('div1Id');
//Gather input values and inputs
var inputValues = [];
var inputs = divToPrint.getElementsByTagName("input");
for (var index = 0; index < inputs.length; index++) inputValues.push(inputs[index]);
//Generate script of page
var script = 'var inputValues = ' + JSON.stringify(inputValues) + ';';
script += 'var inputs = document.getElementsByTagName("input");' +
'for (var index = 0; index < inputs.length; index++) ' +
'inputs[index].value = inputValues[index];';
if (divToPrint != null && divToPrint != "") {
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="' + script + 'window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
newWin.close();
}
Code is untested, please let me know if there is a typo.

Javascript replace a string

I want to change this
[s=http://stackoverflow.com/questions/ask?title=Javascript%20replace%20a%20string]Text[/s]
into this
Text
using javascript
I have try this one.
<script>
var str = "[s=/site_menu.xhtml?get-d=4%2027&get-id=315&get-title=DanMachi%20Season%202&get-main=DanMachi]DanMachi Season 2[/s]";
var res = str.replace("[s=", '<a href="');
var ser = res.replace("[/s]", "</a>");
var serr = ser.replace("]", ':admin-hash-amp:">');
document.write(serr);
</script>
You may want to consider simply creating a function that would encapsulate all of this for you, especially if you plan on using in within multiple areas of your application:
function toHyperLink(input){
return input.replace('[s=','<a href="')
.replace(']','">')
.replace('[/s]','</a>');
}
Example
var input = '[s=http://stackoverflow.com/questions/ask?title=Javascript%20replace%20a%20string]Text[/s]';
console.log(`Input: ${input}`);
console.log(`Output: ${convertToHyperlink(input)}`);
function convertToHyperlink(input) {
return input.replace('[s=', '').replace('[/s]', '');
}
After you do your convert you should create an element instead of write that out.
var str = "[s=http://stackoverflow.com/questions/ask?title=Javascript%20replace%20a%20string]Text[/s]";
var res = str.replace("[s=", '<a href="');
var ser = res.replace("[/s]", "</a>");
var serr = ser.replace("]", '">');
var text = serr.slice(serr.indexOf(">") + 4, serr.indexOf("</a&gt"))
var href = serr.slice(serr.indexOf("href=\"") + 6, serr.indexOf("\"&gt"))
var link = document.createElement("a");
link.text = text;
link.href = href;
document.getElementById("myDIV").appendChild(link);
Try this
var str = "[s=http://stackoverflow.com/questions/ask?title=Javascript%20replace%20a%20string]Text[/s]";
var r = str.replace(/\[s=(.*?)\](.*?)(\[\/s\])/gi,"<a href='$1'>$2</a>");
document.write(r);
I guess you may also do like;
var str = "[s=http://stackoverflow.com/questions/ask?title=Javascript%20replace%20a%20string]Text[/s]",
res = str.replace(/\[s(.+)\](.+)\[\/s\]/, "<a href$1>$2</a>");
console.log(res);

Writing special characters in JavaScript

I am still studying, my iframe is not function as I want, can you provide some advice?
(function($){ function overlay($img, opts){ var floating = opts.float || 'left'; var rgba = opts.rgba || '236,240,241,0.8'; var color = opts.color || '#ffffff'; $img.wrap('
'); $wrap = $img.parent(); $wrap.css('float',floating); $overlay = $('
'); $overlay.addClass('overlay'); $overlay.css('background','rgba('+rgba+')'); $links = $('
'); $overlay.append($links); $facebook = $('
') $facebook.html(''); $links.append($facebook); $overlay.css('height',$img.attr('height'));
$overlay.css('width',$img.attr('width')); $links.css('margin-top', (parseFloat($img.attr('height'))/2.1)+'px'); $overlay.css('margin-top',$img.css('marginTop')); $overlay.css('margin-bottom',$img.css('marginBottom')); $overlay.css('margin-left',$img.css('marginLeft'));
$overlay.css('margin-right',$img.css('marginRight')); $overlay.css('padding-top',$img.css('paddingTop')); $overlay.css('padding-bottom',$img.css('paddingBottom')); $overlay.css('padding-left',$img.css('paddingLeft')); $overlay.css('padding-right',$img.css('paddingRight'));
//Add links $img.before($overlay); } $.fn.socialpic = function(opts){ var opts = opts || []; $(this).each(function(){ overlay($(this), opts); }); return this; } }(jQuery));
Try using the HTML entity code for the special character you want to write. In this case, &bullet;
I ready change spesial carater fungsion overlay only detect txt fungtion
$facebook.html('');

How to get cursor position in rich:editor richfaces by javascript

I have a rich:editor
<rich:editor id="editor" width="1000" height="300"
value="#{emailTemplateHome.instance.emailTemplateContent}"
theme="advanced">
</rich:editor>
and javascript function
function change(){
var val = document.getElementById("#{rich:clientId('editor')}");
if(val != null){
var component = val.component;
var content = component.tinyMCE_editor.getContent();
var position = content.slice(0, val.positionedOffset).length;
var result = content.substr(0,position) + "{chuc_vu}" + content.substr(position);
component.tinyMCE_editor.setContent(result);
}
}
but var position is not defined.
How can i get position of current cursor ???
Thanks in advance
If you only want to insert things at the current position you can use this:
component.tinyMCE_editor.execCommand('mceInsertContent', false, "insert text");
If you want to do something with the cursor position that's going to be tricky, since the position in the editor doesn't correspond to the position in the content (the content has HTML tags in it). You can get the position this way:
component.tinyMCE_editor.selection.getBookmark().start
val.positionedOffset is a function that returns an array, I'm not sure what you were using that for (not to mention that slice(0,x).length = x)
Writing something after cursor position:
function writeAfterCursor(element) {
var bm =tinyMCE.activeEditor.selection.getBookmark().start;
var content= tinyMCE.activeEditor
.getContent();
content= strip(content);
var res = content.split("");
var res1= [];
var res2= [];
for(i=0; i < bm; i++){
res1[i]=res[i];
}
var j=0;
for(i=bm; i < res.length; i++){
res2[j]=res[i];
j++;
}
var content1= res1.join("");
var content2= res2.join("");
content = content1+ element.value+content2;
tinyMCE.activeEditor.setContent(content);
tinyMCE.activeEditor.selection.moveToBookmark(bm);
}
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}

Changing picture in javascript form on a click

In this code I get the following error:Exception... "Index or size is negative or greater than the allowed amount" code: "1" nsresult: "0x80530001 (NS_ERROR_DOM_INDEX_SIZE_ERR)". What is causing this? Thanks
function makecard(){
var bodypaint = document.getElementById('minaj');
var recipient = document.getElementById("recipient").value
var radioboxes = document.forms["cardform"].phrase.length
var i = document.getElementById("color").selectedIndex;
var z = document.getElementById("city").selectedIndex;
var tchatche= document.getElementById("color").options[i].text;
var malouba= document.getElementById("city").options[z].value;
for(c=0; c<radioboxes; c++){
if( document.forms["cardform"].phrase[c].checked){
var phrasevalue=document.forms["cardform"].phrase[c].value;
break;
}
}
if(document.getElementById("color").options[i].text === "White"){
bodypaint.style.backgroundColor ="White"
}
if(document.getElementById("color").options[i].text === "Red"){
bodypaint.style.background ="Red"
}
if(document.getElementById("color").options[i].text === "Blue"){
bodypaint.style.backgroundColor ="Blue"
}
var selectedcity = document.forms["cardform"].city.value
var paragraph = document.createElement("div");
paragraph.setAttribute("id","card")
document.body.appendChild(paragraph)
var picture = document.createElement("img")
picture.setAttribute("src", "")
paragraph.appendChild(picture)
paragraph.appendChild(document.createTextNode(phrasevalue + " from " + selectedcity + recipient))
if(malouba== "Paris"){
document.getElementById("picture").src = "paris.jpg"
}
if(malouba== "Venice"){
document.getElementById("picture").src = "venice.jpg"
}
if(malouba== "Rome"){
document.getElementById("picture").src = "rome.jpg"
}
}
document.getElementById("makeacard").onclick = makecard;
It's highly likely that one of the statements is producing a array of 0 elements and then when you try to use it later it freaks out:
var radioboxes = document.forms["cardform"].phrase.length
do a little console.log(radioboxes) on that and see if it comes back undefined or empty

Categories

Resources