0

I am using Laravel 4

How can I add a link to a image inside a .css or .js file.

If I put a relative path like that:

background-image: url("../assets/images/assets.png");

it will load the file only if my view is like that

/mysite/users

if the url of my view change from

/mysite/users 

to

/mysite/users/job/10/something

It will not load.

I can put the full path like that:

    background-image: url("/mysite/assets/images/assets.png");

but if I have to create a copy of this site like 'theSite', I will have to open All my css files and .js file and replace /mysite to /theSite

Are there a smarty way to do that? like I use in the view:

{{ URL::to('style.css') }}

.

EDITED

The ceejayoz is correct.

But if you need a .js file that call a Controller you could do this:

create this route:

Route::get('/js/custom.js ', function()
{

    $contents = View::make('js/custom');
    $response = Response::make($contents, 200);
    $response->header('Content-Type', 'application/javascript');
    return $response;
});

create the custom.blade.php view and put this js content:

$('#inputX').change(function() {
    $.ajax({                                      
            url: "{{URL::to('track/data')}}",
            type: "GET",
            cache: false,
            async: sync,                         
            data: {
                doit: $('#inputX').val()
            },
            success: function(data)          
            {       
                console.log(data)
            }
    });     

});

What do you think about it?

1 Answer 1

4

URLs in CSS files are relative to the CSS file, not the page it's used on. It should not matter what the URL of the page you're on is - if your CSS is in public/css and you use ../images/something.jpg, it'll use public/images/something.jpg regardless of the URL of the page.

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

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.