0

Javascript developer,

i have this script which i display html dynamically in my template :

$(document).ready(function() {
    var credits= $('body').append('<div id="wrap"><div id="wrapp-inner"><div id="wrapleft"></div><div id="wrapright">Designed Templatezy</div></div></div>');
});

Now i want to add <a expr:href='data:blog.homepageUrl'><data:blog.title/>™</a> to that wrapleft div but its not working to display the blog title and nor it add the url of site.

Hint: this is a <a expr:href='data:blog.homepageUrl'> html/expression tag in blogger template which create link for your site homepage. like http://example.com where as <data:blog.title/> is a tag which display your blog title like "Stack Overflow". it is easy to add in a template but i found it hard when adding it to that append script.

I tried like below but thats not working:

$(document).ready(function() {
    var credits= $('body').append('<div id="wrap"><div id="wrapp-inner"><div id="wrapleft"><a expr:href='data:blog.homepageUrl'><data:blog.title/>™</a></div><div id="wrapright">Designed Templatezy</div></div></div>');
});

So please anyone help me to add this <a expr:href='data:blog.homepageUrl'><data:blog.title/>™</a> after the wrapleft div in that append script. thanks.


Updates:

i can add as general link in the script: like a href stackoverflow.com
$(document).ready(function() {
    var credits= $('body').append('<div id="wrap"><div id="wrapp-inner"><div id="wrapleft"><a href="stackoverflow.com">stackoverflow</a></div><div id="wrapright">Designed Templatezy</div></div></div>');
});

and this work, but how to add <a expr:href='data:blog.homepageUrl'><data:blog.title/>™</a> in the script to work, it is simple to add in template and display title of site with title, but how to add in that script inside append after leftwrap. i add it but it comes as plain texts.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • 1
    You are trying to mix server side templating code with javascript code. They run at different times in different environments – charlietfl Feb 17 '16 at 19:57
  • @charlietfl i am not mixing the side templating code, its i think as an xml code. using in blogger temlpate – Ishaq Counciler Feb 17 '16 at 20:18
  • But their compiler on server would compile it into proper html. I doubt it will do that within your script – charlietfl Feb 17 '16 at 20:20
  • @charlietfl so there is no way to add inside that append after the leftwrapper...any solution to do it...if i add this directly in my template it worked, but it does not work when i add it in after that wrapleft inside append...how to do any solution. – Ishaq Counciler Feb 17 '16 at 20:28
  • You should be adding valid html with correct href value, title etc. I could be wrong...haven't touched anything to do with blogger in many years and I doubt many people here do – charlietfl Feb 17 '16 at 20:31
  • @charlietfl if i could give an idea then i hope you will do it by alternate way...for example we create separate js for those expression like we assign those expression to variable treem=''> then we will add treem in append to display its value..hows that approach ? – Ishaq Counciler Feb 17 '16 at 20:36
  • I suggest you do some research in blogger documentation. There is also a blogger tag that you could tag question with. There probably are more people familiar with this on this site than I was thinking since there would be people here that build templates – charlietfl Feb 17 '16 at 20:38

2 Answers2

0

Since you are using a template language, the template language might be processed before the output reaches the browser. If it is not, if it is somehow handled after it reaches the browser, then jQuery might be attempting to strip unwanted code from .html() and instead may not recognize the tags. You can also use .text() if you want something a little more cut-and-dry.

However, to answer your question in the purest form (via jQuery).

In your second example you wrapped everything in a div tag but this can be accomplished using .wrap() ... what you should do is use the selector to locate what segment of the page you want. Also, please note you wrote "wrapp-inner" not "wrap-inner" ...

Let's go back to your original issue. You want append a link to the bottom of a page. In the doc ready function:

$('body').append("somehtml"); // append to body

However, to maintain sanity, instead you should create a "target" div to populate. Example:

HTML (in body):

<div id="credits"></div>

In Document Ready:

$('#credits').append("somehtml");

See this jsfiddle which demonstrates both. https://jsfiddle.net/83fbnwe4/

And this shows the distinction between .html() and .append, as well as a way to .html() to effectively append:

https://jsfiddle.net/83fbnwe4/2/

Update

To include the other answer with some additional approach, basically you could display it from the jump and hide it with CSS, or hide it with jQuery:

$('#mylink').hide();

If you want to make the href dynamic -- populate it later than the time the code is rendered by your template parser - or if including content based on an AJAX request or simply via javascript event in some way, you can change it after the fact using DOM parsing.

// Simple:
$("#mylinkid").attr('href', 'http://www.live.com/');

//Complicated: 
$("a[href^='http://stackoverflow.com']").each(function() {
 this.href = this.href.replace(/^http:\/\/beta\.stackoverflow\.com/, 
 http://stackoverflow.com");
});

(borrowed from How to change the href for a hyperlink using jQuery #179713)

Community
  • 1
  • 1
  • #Herb wrapp-inner is not the issue...its a name of div....i can add any link to that html, let me updated one example, that i can add link etc...but not those two expressions that i shared,..how to add that after wrapleft div. thanks. – Ishaq Counciler Feb 17 '16 at 20:10
  • check update.i can add any link, but how to add those two expressions to work, when i add it it comes as plain text. not working.. – Ishaq Counciler Feb 17 '16 at 20:17
  • @ishaq-counciler Welp, it's because the plain text is coming through after your template has been parsed. See additions above. – Herb Gilliland Feb 19 '16 at 19:47
0

A possible solution:

You can add <a expr:href='data:blog.homepageUrl'><data:blog.title/>™</a> in your template as:

<div id="blogHomepageUrlLink" style="display: none;">
    <a expr:href='data:blog.homepageUrl'><data:blog.title/>™</a>
</div>

So your template handler can process it.

Then with your jQuery:

$('body').append('<div id="wrap"><div id="wrapp-inner"><div id="wrapleft"></div><div id="wrapright">Designed Templatezy</div></div></div>');

You should be able to add the link with:

$("#wrapleft").html($("#blogHomepageUrlLink").html());

Or combined:

$('body').append('<div id="wrap"><div id="wrapp-inner"><div id="wrapleft">'+$("#wrapleft").html($("#blogHomepageUrlLink").html())+'</div><div id="wrapright">Designed Templatezy</div></div></div>');
Onimusha
  • 3,348
  • 2
  • 26
  • 32
  • your approach is good and you make it possible that it can be done, but here we are still getting error, when adding $("#wrapleft").html($("#blogHomepageUrlLink").html()); to the append body...its not working..however your idea is great...can you try little more why does not it work.. – Ishaq Counciler Feb 17 '16 at 21:25
  • OR, you can assign another link to the #blogHomepageUrlLink Id so it result we see in fiddle...please show me it at fiddle..then that would be good whether it work or not. – Ishaq Counciler Feb 17 '16 at 21:28
  • Did you add the first piece of code in the template file? – Onimusha Feb 17 '16 at 21:32
  • yes i add this code and then the last combined ones...but i am telling you to do it in fiddle..give any static url to id bloghompeulr. like now do this stuff in fiddle so we see that it work or not...if it work in fiddle then i hope the problem will be solved...like this ones: https://jsfiddle.net/ocau9L1z/ but it not working, if you make this to work. – Ishaq Counciler Feb 17 '16 at 21:46
  • i does not work i checked with static url assing to that id. https://jsfiddle.net/83fbnwe4/4/ – Ishaq Counciler Feb 17 '16 at 22:22
  • See the update of your fiddle with the corrected syntax https://jsfiddle.net/83fbnwe4/14/ – Onimusha Feb 18 '16 at 11:26