Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:27:37+00:00 2026-06-18T09:27:37+00:00

I found a few questions on here relating to my question but didn’t exactly

  • 0

I found a few questions on here relating to my question but didn’t exactly get the answer I was looking for. I am wanting to do something like this, similar to what jQuery often does:

createCSS("mystyle.css", {
    media: "screen",
    type: "text/css"
});

I tried this to accomplish what I want but it’s not working:

var prop = {
    media: '',
    type: ''
};

function createCSS(href, prop) {
    var anchor = document.createElement("link");
    anchor.setAttribute("href", href);
    anchor.setAttribute("media", prop.media);
    anchor.setAttribute("type", prop.type);

    if(typeof anchor != "undefined") {
        document.getElementsByTagName("head")[0].appendChild( anchor );
    }
}

Now, I know I could just create single multiple parameters like createCSS("mystyle.css", "screen", "text/css"); but I don’t like that, the other way looks cooler.

Plenty new to javascript so any help would be very much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:27:38+00:00Added an answer on June 18, 2026 at 9:27 am

    You don’t have to declare/initialize var prop. Your function looks fine, just call it passing an object as prop, as in your own example:

    createCSS("mystyle.css", {
        media: "screen",
        type: "text/css"
    });
    

    If the intention with the var prop part was to avoid assigning undefined to the attributes, you need a little tweak inside the function:

    function createCSS(href, prop) {
        prop = (typeof prop !== "object") ? {} : prop;
        prop.media = prop.media || 'screen';  // default media will be screen
        prop.href = prop.href || 'text/css';  // default type will be text/css
        // rest of code
    }
    

    Some minor improvements I’d suggest:

    • Your variable anchor does not contain an anchor (<a>) element. Why not call it link?
    • You don’t seem to need the if(typeof anchor != "undefined") conditional. since you’re creating the element a few lines above, that variable will never be undefined. You can skip the if and appendChild directly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i found few similar questions here but from answers i didn't get the whole
I found a few questions related to this around here but not exactly what
I found a few questions similar to this one here on SO, but none
I've asked a few questions about this, but haven't found the answer (or maybe
I've looked at a few questions on here and I never found an answer
I've found a few questions on this subject here but their answers do not
I have found a few similar questions here in Stack Overflow, but I believe
There are a few questions on here that are similar, but they didn't work
I have found a few different questions that pertain to my question, but I'm
I've found a few other people asking this question, but the answers for their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.