0

How would you be able to inject a css link thats in the middle of the page to the head using javascript

<head>
  ... styles here
</head>
<body>
 code
 <link href='http://fonts.googleapis.com/css?family=Lato:400,300,700,900' rel='stylesheet' type='text/css'>
</body>

i need some sort of javascript to push the google fonts into head, what would be the easiest way?

3
  • What would be the point? If you can add JavaScript to the page, why can't you manually move the CSS link? No to mention the fact that by the time the JS has found the link, the link should have already been loaded. Commented Aug 10, 2015 at 14:05
  • i only have access to part of the code so not the head, also it was to try and solve another problem this font doesnt seem to work in firefox Commented Aug 10, 2015 at 14:08
  • Try giving this a read: stackoverflow.com/a/6220566/3756866 - you could probably have it added after your <title> Commented Aug 10, 2015 at 14:08

3 Answers 3

0
$('head').append("<link href='http://fonts.googleapis.com/css?family=Lato:400,300,700,900' rel='stylesheet' type='text/css'>");

Try this and let me know if this worked.

Sign up to request clarification or add additional context in comments.

Comments

0

Something as simple as ....

$("head").append($("link").clone());

You could make your selector more specific if you have other link elements

Comments

0

In javascript you can do it like this :

<script type="text/javascript">

$('head').append("link");

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.